Reputation: 67
guys help me please with it. I don't know how to operate with regex, I read a lot about this, but anyway, I don't understand how it works.
This is my problem: I have code like this:
var str = "<strong>What do you think</strong> if we could just buy this <strong>Robot</strong>";
str = str.match(/[<strong>][a-z\s]+[<\/strong>]/gi);
And after code is done, I get something like this: >What do you think<,>Robot<
But I waited for this: What do you think, Robot.
What's wrong? It drives me crazy.
UPDATE: Thanks to all of you!!! Now, solution is found
Upvotes: 0
Views: 214
Reputation: 14069
This is how its done in most (modern) regex dialects
.*?(?<=<strong>)(.*?)(?=</strong>).*?|.*
replace with
$1
Unfortunately, Javascript does not support lookbehind. If you are up for it you can read http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript and give it a go. Pity js doesn't support such a powerful technique
Upvotes: 0
Reputation: 32286
If you don't feel like fussing with RegExp.exec()
, use this:
var matches = str.match(/<strong>.*?<\/strong>/gi);
var result = [];
for (var i = 0; i < matches.length; ++i)
result.push(matches[i].match(/<strong>(.*)<\/strong>/i)[1]);
Upvotes: 0