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
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
Share Improve this question edited May 15, 2017 at 16:13 cнŝdk 32.1k7 gold badges60 silver badges80 bronze badges asked May 15, 2017 at 15:39 user3872094user3872094 3,3499 gold badges38 silver badges78 bronze badges 3-
var newString = textToReplace.replace(/<b.*?\/b>\s*/g, '');
. Regex101 example. Read about the global modifier and the non-greedy regexps. – ibrahim mahrir 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. – user3872094 Commented May 15, 2017 at 15:50
-
1
var newString = textToReplace.replace(/<b.*?>(.*?)<\/b>/g, '$1');
. Regex101 example. – ibrahim mahrir Commented May 15, 2017 at 15:52
4 Answers
Reset to default 9var 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.
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/7cgb51ju/
I would use something like this :
textToReplace.replace(/<b class =\"b_1\">[0-9]+<\/b>/g, '');
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 (.*?)
.