Reputation: 17
I need information about finding a word and replacing it with regular expressions in javascript.
Upvotes: 0
Views: 137
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
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