rajasekhar
rajasekhar

Reputation: 17

Finding a word and replacing using regular expressions in javascript

I need information about finding a word and replacing it with regular expressions in javascript.

Upvotes: 0

Views: 137

Answers (2)

Guffa
Guffa

Reputation: 700272

You can use \b to specify a word boundary. Just put them around the word that you want to replace.

Example:

var s = "That's a car.";
s = /\ba\b/g.replace(s, "the");

The variable s now contains the string "That's the car". Notice that the "a" in "That's" and "car" are unaffected.

Upvotes: 3

Nicole
Nicole

Reputation: 33197

Since you haven't really asked a question, the best I can do is point you here:

http://www.w3schools.com/jsref/jsref_obj_regexp.asp

And tell you also that the replace method for a string is replace, as in:

var myStr = "there is a word in here";
var changedStr = myStr.replace(/word/g, "replacement");

Upvotes: 0

Related Questions