[thelist] regex problem

Matt Warden mwarden at gmail.com
Mon Oct 10 08:13:11 CDT 2005


On 10/10/05, Tom Dell'Aringa <pixelmech at yahoo.com> wrote:
> My weekly regex conundrum! I have a string where I want to strip out any occurance of a wildcard
> '*'. I am attempting this:
>
> var strippedNameValue = nameValue.replace(/^[\*]$/g, "");
>
> with the test string "t**"
>
> This does not work, I always end up with the same string, it is not replacing the *. I have
> escaped the * but it does not seem to match it - what am I doing wrong?

You don't necessarily need a regex for this. But, anyway, the problem
is that you are using ^ and $. So, essentially you are trying to match
the string '*', because you are saying that the string must start,
have one character which is *, and then end. Anything other than the
stirng '*' will fail.

Remove the boundaries and you should be okay.

var strippedNameValue = nameValue.replace(/[\*]/g, "");

--
Matt Warden
Miami University
Oxford, OH, USA
http://mattwarden.com


This email proudly and graciously contributes to entropy.


More information about the thelist mailing list