Leslie Jones Leslie Jones - 2 years ago 145
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 Source

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' 



' 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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download