8

I'm having the below string in my node js.

var textToReplace = "Your <b class =\"b_1\">1</b> payment due is $4000.Your 
<b class =\"b_1\">2</b> payment due is $3500. Your <b class =\"b_1\">3</b> 
payment due is $5000.";

Here I want to replace <b class =\"b_1\">*</b> with ''. The output is Your 1 payment due is $4000.Your 2 payment due is $3500. Your 3 payment due is $5000..

If this is a normal replace I wouldn't have had any problem, but here I think the best way to replace is by using Regex. This is where I'm confused. In java we have a stringVariableName.replaceAll() method. please let me know how can I do this.

Thanks

3
  • var newString = textToReplace.replace(/<b.*?\/b>\s*/g, '');. Regex101 example. Read about the global modifier and the non-greedy regexps. Commented May 15, 2017 at 15:45
  • @ibrahimmahrir, Thanks for the quick reply. my bad, I actually forgot the actual replaced string, can you please look into my updated question. Commented May 15, 2017 at 15:50
  • 1
    var newString = textToReplace.replace(/<b.*?>(.*?)<\/b>/g, '$1');. Regex101 example. Commented May 15, 2017 at 15:52

4 Answers 4

10
var newString = textToReplace.replace(/<b.*?>(.*?)<\/b>/g, '$1');

Explanation:

<b.*?> : matches the <b ...> opening tag (using the non-greedy quantifier to match as few as possible)
(.*?)  : matches the content of the <b></b> tag (should be grouped so it will be used as a replacement text), it uses the non-greedy quantifier too.
<\/b>  : matches the closing tag
g      : global modifier to match as many as possible

then we replace the whole match with the first captured group $1 which represents the content of the <b></b> tag.


Example:

var str = "Your <b class =\"b_1\">1</b> payment due is $4000.Your <b class =\"b_1\">2</b> payment due is $3500. Your <b class =\"b_1\">3</b> payment due is $5000.";

var newString = str.replace(/<b.*?>(.*?)<\/b>/g, "$1");

console.log(newString);


Regex101 example.

Sign up to request clarification or add additional context in comments.

Comments

0

You don't necessarily need a RegEx for this but using jQuery you can easily remove the children and return the HTML using a classic one-liner:

textReplaced = $('<div>').html(textToReplace).children().remove().end().html();

A working fiddle here - https://jsfiddle.net/7cgb51ju/

1 Comment

ehem ehem. He said nodeJS.
0

I would use something like this :

textToReplace.replace(/<b class =\"b_1\">[0-9]+<\/b>/g, '');

Comments

0

You just need to use Regex capturing groups with the following Regex /<b.*?>(.*?)<\/b>/g, you can then use it in your code like this:

var textToReplace = "Your <b class =\"b_1\">1</b> payment due is $4000.Your <b class =\"b_1\">2</b> payment due is $3500. Your <b class =\"b_1\">3</b> payment due is $5000.";


console.log(textToReplace.replace(/<b.*?>(.*?)<\/b>/g, '$1'));

And here '$1' will preserve the first captured group for (.*?).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.