Find a way to find characters that doesn't match the Regex is javascript - Stack Overflow

admin2025-04-21  0

I am trying to find a way to determine which character in my string does not match the regex, is there a way to do so in Javascript?

I've been using the regex object and i was able to determine whether a string matches the regex but i would like to go step further to determine why a string does not match the regex.

any thoughts?

This was what I currently have ... i am just trying to make sure a string only contains the set of characters found in the following regex ... and i would like to see which character does not match.

Here's my code :

var regexTest = new RegExp("^[0-9a-zA-Z\\!\\040\\@\\s\\#\\$\\%\\&\\*\\(\\)\\_\\+\\:\\\"\\<\\>\\?\\-\\=\\;\\'\\,\\.\\\\]+$",g);

var bValid = regexTest.test(value); //this will check whether the value is valid ... 

I've tried using value = value.replace(regexTest,''), but was unable to actually filter out the characters.

I am trying to find a way to determine which character in my string does not match the regex, is there a way to do so in Javascript?

I've been using the regex object and i was able to determine whether a string matches the regex but i would like to go step further to determine why a string does not match the regex.

any thoughts?

This was what I currently have ... i am just trying to make sure a string only contains the set of characters found in the following regex ... and i would like to see which character does not match.

Here's my code :

var regexTest = new RegExp("^[0-9a-zA-Z\\!\\040\\@\\s\\#\\$\\%\\&\\*\\(\\)\\_\\+\\:\\\"\\<\\>\\?\\-\\=\\;\\'\\,\\.\\\\]+$",g);

var bValid = regexTest.test(value); //this will check whether the value is valid ... 

I've tried using value = value.replace(regexTest,''), but was unable to actually filter out the characters.

Share Improve this question edited Feb 9, 2012 at 22:31 Blender 299k55 gold badges458 silver badges510 bronze badges asked Feb 9, 2012 at 17:10 aggietechaggietech 9783 gold badges17 silver badges38 bronze badges 1
  • 3 I don't think this is really well defined in general. Which character in "aababb" doesn't match the regex /^a*b*$/? Which character in "aaaccc" doesn't match the regex /^a+b+c+$/? Which character in "" (ie: the empty string) doesn't match the regex /a+/? – Laurence Gonsalves Commented Feb 9, 2012 at 17:14
Add a ment  | 

1 Answer 1

Reset to default 6

You could replace all the characters that do match with '', leaving only the things that don't match:

'abc123'.replace(/([a-z]+)/g, '')
// "123" 
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745177855a289010.html

最新回复(0)