0

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
rajasekhar
  • 17
  • 4

2 Answers2

3

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.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

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");
Nicole
  • 32,841
  • 11
  • 75
  • 101
  • @Renesis: please, w3schools.com is awful, post a better link. How about this one: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace – Sean Patrick Floyd Dec 20 '10 at 08:52
  • @Sean Patrick Floyd thanks for the other link. Not sure I agree that it's awful, even if it's usually light on explanation, it is a succinct reference for syntax. – Nicole Dec 20 '10 at 08:54
  • @Renesis you can find some discussion about w3schools in this question: http://stackoverflow.com/questions/4312945/where-to-find-javascript-documentation – Sean Patrick Floyd Dec 20 '10 at 08:59
  • @Sean - I don't see any discussion there - and again, when googling I don't often come up with any top 10 results that are as succinct as w3schools. – Nicole Dec 20 '10 at 09:07