js替换中文字符串问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js替换中文字符串问题相关的知识,希望对你有一定的参考价值。

msg = msg+''.replace(/元整 /,"");
alert(msg);
msg是大写的金额。想把元整替换成空,但是不起作用,跟编码有问题?
错了是这样写的:

msg = msg.replace(/元整 /,"");
alert(msg);

javascript中replace() 方法如果直接用str.replace("-","!") 只会替换第一个匹配的字符.
而str.replace(/\\-/g,"!")则可以全部替换掉匹配的字符(g为全局标志)。
replace()
The replace() method returns the string that results when you replace text matching its first argument
(a regular expression) with the text of the second argument (a string).
If the g (global) flag is not set in the regular expression declaration, this method replaces only the first
occurrence of the pattern. For example,
var s = "Hello. Regexps are fun." ;s = s.replace(/\\./, "!" ); // replace first period with an exclamation pointalert(s);
produces the string “Hello! Regexps are fun.” Including the g flag will cause the interpreter to
perform a global replace, finding and replacing every matching substring. For example,
var s = "Hello. Regexps are fun." ;s = s.replace(/\\./g, "!" ); // replace all periods with exclamation pointsalert(s);
yields this result: “Hello! Regexps are fun!”
所以可以用以下几种方式.:
string.replace(/reallyDo/g, replaceWith);
string.replace(new RegExp(reallyDo, \'g\'), replaceWith);
string:字符串表达式包含要替代的子字符串。
reallyDo:被搜索的子字符串。
replaceWith:用于替换的子字符串。
Js代码
<script type="text/javascript">
String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase)
if (!RegExp.prototype.isPrototypeOf(reallyDo))
return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith);
else
return this.replace(reallyDo, replaceWith);


</script>
参考技术A 没问题啊。var msg = "伍拾元整";
msg = msg.replace(/元整/,\'\')
alert(msg);

追问

不知道怎么,我用msg.replace(‘元整’,'')就解决了

本回答被提问者采纳
参考技术B replaceAll("(?i)[^a-zA-Z0-9\u4E00-\u9FA5]", "");
这个不行 把所有的都替成空了应该
你截取字符串不行么
从后面两位开始截取 一直截到最前
或者从最前开始 截到倒数两位

在js中,如何替换一个文本中的多个字符?

在js中,譬如var text="我是中国人,你呢?我是大好人,你呢?";那么要将text里面的“我”全部替换成“他”?该怎么替换呢?我使用replace方法,结果替换的只有第一个“我”字,后面的就不行了,应该写怎样的正则表达式才可以解决这个问题呢?

js中提供了replace方法进行字符串替换:
replace()
replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。
stringObject.replace(regexp/substr,replacement)
regexp/substr是规定的子字符串或要替换的模式的 RegExp 对象。请注意,如果该值是一个字符串,则将它作为要检索的直接量文本模式,而不是首先被转换为 RegExp 对象。replacement是一个字符串值。规定了替换文本或生成替换文本的函数。返回一个新的字符串,是用 replacement 替换了 regexp 的第一次匹配或所有匹配之后得到的。
字符串 stringObject 的 replace() 方法执行的是查找并替换的操作。它将在 stringObject 中查找与 regexp 相匹配的子字符串,然后用 replacement 来替换这些子串。如果 regexp 具有全局标志 g,那么 replace() 方法将替换所有匹配的子串。否则,它只替换第一个匹配子串。
replacement 可以是字符串,也可以是函数。如果它是字符串,那么每个匹配都将由字符串替换。但是 replacement 中的 $ 字符具有特定的含义。如下表所示,它说明从模式匹配得到的字符串将用于替换。

字符
替换文本

$1、$2、...、$99 与 regexp 中的第 1 到第 99 个子表达式相匹配的文本。
$& 与 regexp 相匹配的子串。
$` 位于匹配子串左侧的文本。
$\' 位于匹配子串右侧的文本。
$$ 直接量符号。

ECMAScript v3 规定,replace() 方法的参数 replacement 可以是函数而不是字符串。在这种情况下,每个匹配都调用该函数,它返回的字符串将作为替换文本使用。该函数的第一个参数是匹配模式的字符串。接下来的参数是与模式中的子表达式匹配的字符串,可以有 0 个或多个这样的参数。接下来的参数是一个整数,声明了匹配在 stringObject 中出现的位置。最后一个参数是 stringObject 本身。
var str = "Visit Microsoft!"
console.log(str.replace(/Microsoft/, "W3School"))

结果是Visit W3School!。
参考技术A 方法一: text.replace("我","他"); 方法二: 在程序的加载事件,用text=Match.Replace("源码","我","他"); 参考技术B text.replace(/我/g,"他"); 参考技术C 直接删了就行了。

以上是关于js替换中文字符串问题的主要内容,如果未能解决你的问题,请参考以下文章

如何在js中一一替换所有相同的字符? [复制]

在js 中 replace 怎么不能替换中文?

关于js怎样替换反斜杠和单引号的问题

替换 Discord.JS 中的某些字符

在js中,如何替换一个文本中的多个字符?

js 替换 中文 汉字 字符串