Pages

Tuesday, 23 October 2012

Find and Replace all in jquery


             I was looking for find and replace all  in jquery, but I found that javascript have very nice library for regular expression.Also it is compatible with all the browser. Below is syntax and simple example:

Syntax :

outputString = searchIn.replace(new RegExp("YourExpression"'g'),"String to Replaced");

or 

strings strings.replace(/hi/g," bye");

where /g represents all the occurrences of matched string. 


Try out example :

Click  buttons on menu bar to run script. Also using + button you can edit script and test as per need :
 

2 comments:

  1. Actually replace() function is work with only first occurrence so use this very simple code.

    str.replace(/\ /g, '-');

    This code will replace all spaces with -

    I code this code from http://www.tutorialhall.com/2015/10/how-to-replace-all-occurrence-of-string-jquery-javascript.html

    ReplyDelete
  2. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog.
    Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from javascript and jquery training in chennai .
    or learn thru Javascript Training in Chennai.
    Nowadays JavaScript has tons of job opportunities on various vertical industry. javascript and jquery training in chennai

    ReplyDelete