Jils
Jils

Reputation: 783

jquery - replace matched character

I want to replace a comma followed by a < br > so I used regex et replace method to do that:

var str = "<a class=added> ,,, ,, blbl;test, </a><br>, <a class=added>"
str = str.replace(/br>(,\s)/g, " "); 
alert(str);

In the result I noticed that 'br>' was also removed and it is not the exected result. Is there anything wrong with my regex?

"<a class=added> ,,, ,, blbl;test, </a>< <a class=added>"

Upvotes: 0

Views: 38

Answers (2)

Jaykumar Patel
Jaykumar Patel

Reputation: 27624

You want to remove all comma followed by a white space by use following Pattern,

Check this Demo Pattern

Pattern

/(\s,+)/g

Check this Demo jsFiddle

jQuery

var str = "<a class=added> ,,, ,, blbl;test, </a><br>, <a class=added>"
str = str.replace(/(\s,+)/g, " "); 
alert(str);

Result

<a class=added>   blbl;test, </a><br>, <a class=added> 

Upvotes: 1

sshashank124
sshashank124

Reputation: 32207

You are doing the regex match correctly however you are not replacing correctly. This should be your replace string:

"br> "

as follows:

>>> str = str.replace(/br>(,\s)/g, "br> ");
"<a class=added> ,,, ,, blbl;test, </a><br> <a class=added>"

Upvotes: 1

Related Questions