简体   繁体   English

替换所有出现的*字符串

[英]Replace all occurrences of * a string

I am trying to do string.replace() on the * character. 我正在尝试在*字符上执行string.replace() The string has multiple occurrences of the character and so I need to do a global replace, but if I do what seems natural it produces the comment tag. 该字符串有多次出现的字符,因此我需要进行全局替换,但是如果我执行看起来很自然的操作,它将生成注释标记。

x.replace(/*/g, '');

How do you work around this? 您如何解决这个问题?

You'll need to escape the * 您需要转义*

* is a reserved lookup item: *是保留的查找项目:

* Matches the preceding character 0 or more times. *与前面的字符匹配0次或更多次。 Equivalent to {0,}. 等效于{0,}。

For example, /bo*/ matches 'boooo' in "A ghost booooed" and 'b' in "A bird warbled", but nothing in "A goat grunted". 例如,/ bo * /匹配“ A booooed”中的“ boooo”和“ A bird warbled”中的“ b”,但匹配“ Aunted grunt”中的任何内容。

See Here 看这里

var jamie = '* 8 * *';
jamie = jamie.replace(/\*/g, '');
// 8 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM