Tom Gullen
Tom Gullen

Reputation: 61737

Help with Regexp

Given the test string:

<div class="comment-quoter">Comment by <strong>Tom</strong>

I want to change it to

[quote=Tom]

I've got as far as this but it gets no matches:

PostTxt = PostTxt.replace(new RegExp("<div class=\"comment-quoter\">Comment by <strong>{(.+),}</strong>", "g"), '[quote=$1]')

Upvotes: 1

Views: 134

Answers (3)

Town
Town

Reputation: 14906

Try:

PostTxt = PostTxt.replace(new RegExp("<div class=\"comment-quoter\">Comment by <strong>(.+)</strong>", "g"), '[quote=$1]')

The round brackets denote the $1 capture group, so the curly brackets and comma would match literals and aren't necessary.

Dependent on what you're expecting, you can make it less greedy by being more specific about the characters you're matching for the capture group:

(\w+)

would match one or more alpha-numeric characters and would return correct matches if you have more than one quote in your input string.

Upvotes: 5

BraedenP
BraedenP

Reputation: 7215

If you want to do it without the overhead of explicitly creating a new RegExp object (since you're not storing it anyway) just do this:

PostTxt = PostTxt.replace(/<div class="comment-quoter">Comment by <strong>(.+)<\/strong>/g, '[quote=$1]');

Upvotes: 1

Jason McCreary
Jason McCreary

Reputation: 72981

PostTxt = PostTxt.replace(/<div class="comment-quoter">Comment by <strong>(.+?)<\/strong>/g, '[quote=$1]')

Upvotes: 0

Related Questions