Saturday 15 February 2014

javascript - Match the character * and not the \* in regex? -



javascript - Match the character * and not the \* in regex? -

i have next string item * \* , *. match * characters , skip \* one.

var input = "item * \\* , *"; var output = (some regex magic happens here) alert(output); // item foo \* , foo

any ideas?

here's think you're trying acheive:

class="snippet-code-js lang-js prettyprint-override">var input = "item * \\* , *"; var repl = "foo"; var output = input.replace(/(\\\*)|\*/g, function(_, a) { homecoming || repl; }); alert(output); // item foo \* , foo

basically, when provide function replace method, pass in match , matched groups arguments function , utilize homecoming value replacement string. in case, since argument a has value when matches "\*", leave match unmodified. otherwise, replace "foo".

javascript regex

No comments:

Post a Comment