Difference between g and +

Tell us what’s happening:
Describe your issue in detail here.

I don’t have a question about the code to pass the challenge but I just need clarification on the difference between the functions of g and +. In a previous challenge, there was an example where g would match [“Repeat”, “repeat”, “repeat”], if we are adding g after /, then what is the point of using +?
Your code so far


let difficultSpelling = "Mississippi";
let myRegex = /change/; // Change this line
let result = difficultSpelling.match(myRegex);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36

Challenge: Match Characters that Occur One or More Times

Link to the challenge:

g is used when more then one patterns are present in the whole string.

example:-
let spelling = “Mississippi”;
let regex= /s/g;
let result = spelling.match(regex);
output:[“s”, “s”, “s”, “s”]

+(plus) is used when at least one or more patterns are present consecutively.

example:-
let spelling = “Mississippi”;
let regex= /s+/g; // g is also used here bcz it’s checking if more than one ss(consecutively) are present in a row
let result = spelling.match(regex);
output:[ “ss” , “ss” ]

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.