Javascript - 如何替换子字符串?

时间:2022-09-13 07:48:21

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

这很简单。我想使用Javascript在客户端用另一个子字符串替换子字符串。

Original string is 'original READ ONLY'

原始字符串是'原始只读'

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

我想用'READ WRITE'替换'READ ONLY'

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

请问快速回答?可能有一个javascript代码片段......

4 个解决方案

#1


25  

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).

String.replace()是基于regexp的;如果传入一个字符串作为第一个参数,则由它生成的正则表达式将不包含'g'(全局)标志。如果要替换搜索字符串的所有出现(通常是您想要的),此选项是必不可少的。

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.

如果查找字符串可能包含在regexp中具有不需要的特殊含义的字符,则这是首选。

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

无论如何,对于问题中的示例,任何一种方法都可以。

#2


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.

好总结。它是基于regexp的,如果使用regexp表示法,你可以指定i和g修饰符(不区分大小写(i),它将匹配case和global(g),它将替换所有出现的),如果你使用字符串表示法它将转换为正则表达式,您将无法指定任何修饰符。

<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>

#3


3  

stringObject.replace(findstring,newstring)

#4


2  

I prefer the regex approach,

我更喜欢正则表达式方法,

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

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.

它也值得考虑g和i正则表达式修饰符,它们做全局替换(即替换所有出现)并使其不区分大小写。

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>

#1


25  

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).

String.replace()是基于regexp的;如果传入一个字符串作为第一个参数,则由它生成的正则表达式将不包含'g'(全局)标志。如果要替换搜索字符串的所有出现(通常是您想要的),此选项是必不可少的。

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.

如果查找字符串可能包含在regexp中具有不需要的特殊含义的字符,则这是首选。

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

无论如何,对于问题中的示例,任何一种方法都可以。

#2


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.

好总结。它是基于regexp的,如果使用regexp表示法,你可以指定i和g修饰符(不区分大小写(i),它将匹配case和global(g),它将替换所有出现的),如果你使用字符串表示法它将转换为正则表达式,您将无法指定任何修饰符。

<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>

#3


3  

stringObject.replace(findstring,newstring)

#4


2  

I prefer the regex approach,

我更喜欢正则表达式方法,

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

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.

它也值得考虑g和i正则表达式修饰符,它们做全局替换(即替换所有出现)并使其不区分大小写。

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>