Leslie Jones Leslie Jones - 6 months ago 16
Javascript Question

Javascript Regex Resources

Im trying to decode this regex statement in javascript, im new to regex and cant find many good resources.

I think the below means find any characters between two single quotes is that correct and what does each character mean in this statement?

From what i understand so far is the first / and last / tell js to look for a pattern.

Can anyone break this regex down into pieces?

mystring = mystring.match(/'([^']+)'/)[0];

Answer

Yes you are right, this finds the word or a character that is in between the single quotes. you can view the below code for example.

var mystring ="This is 'some' random 'test' with string";
mystring = mystring.match(/'([^']+)'/)[0];
alert(mystring); // This alerts 'some' 

Explanation:

/'([^']+)'/

' matches the character ' literally

1st Capturing group ([^']+)

[^']+ match a single character not present in the list below

Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]

' the literal character '

' matches the character ' literally

Comments