14

This is a simple one. I want to replace a sub-string with another sub-string on client-side using Javascript.

Original string is 'original READ ONLY'

I want to replace the 'READ ONLY' with 'READ WRITE'

Any quick answer please? Possibly with a javascript code snippet...

3
  • 8
    I am not sure you are being very honest. The question could be a 'no-brainer' but it is the little things that matter when one is implementing some functionality. Esp things to do with string manipulation. I feel Stackoverflow is best suited for such. So please do not prejudice. Commented Oct 31, 2008 at 16:08
  • You were asking how to replace a defined word in a defined string. I can't see any implementation-dependent subtleties in that, it is just straight and dead-simple. No offense, there is nothing wrong with asking that kind of questions. It's something wrong with up-voting them. Commented Oct 31, 2008 at 19:15
  • 2
    Your complaint is groundless. The alt text on the up arrow image says "This question is useful and clear..." which is a subjective matter. Now, if it said "This question is useful and clear to Ambassador Tomalak..." there might be grounds for your complaint. Commented Jul 23, 2010 at 8:00

4 Answers 4

27

String.replace() is regexp-based; if you pass in a string as the first argument, the regexp made from it will not include the ‘g’ (global) flag. This option is essential if you want to replace all occurances of the search string (which is usually what you want).

An alternative non-regexp idiom for simple global string replace is:

function string_replace(haystack, find, sub) {
    return haystack.split(find).join(sub);
}

This is preferable where the find string may contain characters that have an unwanted special meaning in regexps.

Anyhow, either method is fine for the example in the question.

Sign up to request clarification or add additional context in comments.

Comments

12

Good summary. It is regexp based, if you use regexp notation you can specify the i and g modifiers (case insensitive (i), which will match regardless to case and global (g), which will replace all occurences), if you use string notation it'll get converted to a regex and you wont' be able to specify any modifier.

<script type="text/javascript">

var str1="Visit Microsoft!";
var str2 = str1.replace(/microsoft/i, "W3Schools"); //Will work, per the i modifier 

var str3 = "original READ ONLY";
var str4 = str3.replace("ONLY", "WRITE"); //Will also work

</script>

2 Comments

str2 = str2.replace(/^Visit/, 'Don't visit');
bobince's answer is the more correct; this answer implies that passing a string is not the same as the regex, but as bobince indicates, either way it is used as a regex.
3
stringObject.replace(findstring,newstring)

Comments

2

I prefer the regex approach,

newstring = oldstring.replace(/regexforstringtoreplace/, 'new string');

its also worth considering the g and i regex modifiers, these do a global replace (i.e. replaces all occurrences) and makes it case insensitive.

for example:

<script type="text/javascript">

var str = "this is a String";

document.write(str.replace(/\s/g, "_"));

would print: this_is_a_string

document.write(str.replace(/s/gi, "f"));

would print "thif if a ftring"

</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.