Rishi Php Rishi Php - 6 months ago 13
Javascript Question

Regx Replace issue - Jquery

I tried use regx replace method to replace

xxx="yyy"
pattern of text in given string like.

my pattern is :
/^[a-zA-Z0-9.;:|_-]+="[a-zA-Z0-9.;:_-]+\"/


Code:

var userinput = '<div id="c16430" style="color:red;" class="css-btn">';

var pattern = /^[a-zA-Z0-9.;:|_-]+="[a-zA-Z0-9.;:_-]+\"/;

userinput = userinput.replace( pattern, "Replaced..." );


But its not working... jsfiddle

Where i done mistake. Please help to resolve this.!!

Thanks in advance..

Answer

You have a couple of problems:

  1. You are trying to match the start of the input, using ^ at the start
  2. You are not using the global flag /g at end, so it would only replace the first match.

This will work:

var pattern = /[a-zA-Z0-9.;:|_-]+="[a-zA-Z0-9.;:_-]+\"/g;

Here is your updated example

Comments