-2

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];
Leslie Jones
  • 472
  • 2
  • 7
  • 20
  • https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions http://www.regular-expressions.info/ http://regex101.com/ – zerkms May 27 '16 at 01:14
  • Look at the return type for [`.match()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match). It returns an array or null. – 4castle May 27 '16 at 01:16
  • 2
    Give a look at the top-right panel in this site: _https://regex101.com/r/vC6eW1/1_ – Washington Guedes May 27 '16 at 01:18
  • 1
    There's no good resources for regex.. It's only practice.. – choz May 27 '16 at 01:26
  • I wrote the following book, free and it might be of help. https://www.syncfusion.com/resources/techportal/details/ebooks/regularexpressions – Sparky May 27 '16 at 01:48

1 Answers1

1

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