Geronimo
Geronimo

Reputation: 57

Regex need to includes the plus sign

I have the following problem with my regex.

I want to search a string between two strings.

The datas is like that:

var datas = "a='00-8'b='13-'a+='00-2'b+='3333'c='4'";

I try:

datas.match("a\+='(.*?)'");

I can't get the regex working due to the + sign.

Any help ?

Upvotes: 0

Views: 79

Answers (2)

Paul S.
Paul S.

Reputation: 66324

You're passing a String into match, not a RegExp, perhaps you wanted

datas.match(/a\+='(.*?)'/);

Alternatively, you need to escape your backslash for the String so it can escape the + as a RegExp, i.e.

datas.match("a\\+='(.*?)'");

Upvotes: 3

Avinash Raj
Avinash Raj

Reputation: 174696

Enclose the regex within forward slashes.

datas.match(/a\+='(.*?)'/g);

OR

Escape the backslash one more time, if it's enclosed within double quotes.

> datas.match("a\\+='(.*?)'");
[ 'a+=\'00-2\'',
  '00-2',
  index: 15,
  input: 'a=\'00-8\'b=\'13-\'a+=\'00-2\'b+=\'3333\'c=\'4\'' ]
> datas.match("a\\+='(.*?)'")[1];
'00-2'

Upvotes: 1

Related Questions