tpie
tpie

Reputation: 6221

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?

Upvotes: 0

Views: 58

Answers (1)

Jamie Hutber
Jamie Hutber

Reputation: 28096

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 

Upvotes: 3

Related Questions