Thursday, April 21, 2011

javascript Isolating characters

I have a string of characters and want to isolate 2 characters.

Isolate a or b from string:

"a48y jvu5agbl=dbz'bk;ahc"

Should get something like this:

aabbba

This regex gets me the correct result and whatever's left. How do I chop off the end?

str.replace(/.*?(b|a)/g,"$1");

Thanks. Any other way to isolate the characters is great too.

Thanks.

From stackoverflow
  • You might have an easier time with [^...] to grab everything but and remove it:

    str.replace(/[^ab]+/g, '');
    

0 comments:

Post a Comment