简体   繁体   中英

Replace all occurrences of * a string

I am trying to do string.replace() on the * character. 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. Equivalent to {0,}.

For example, /bo*/ matches 'boooo' in "A ghost booooed" and 'b' in "A bird warbled", but nothing in "A goat grunted".

See Here

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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