Rishi Php Rishi Php - 5 months ago 9
Javascript Question

Regx Replace issue - Jquery

I tried use regx replace method to replace

pattern of text in given string like.

my pattern is :


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..


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