-2

I have a regex in a variable

The purpose of my regex is to list down all of allowable characters for user input

Note that the regex may change from time to time depending on the user preference

var regex = new RegExp(passParamStateMap.value, "g");

The value of my regex after console.log is

/~@#$%^&*()_+{}[]:;<>,.?\//g

However, when I try to match it with user input, I am getting null value

Can someone enlighten me please.

var regex = new RegExp(passParamStateMap.value, "g");

     if( e.target.value.match(regex) ){ //null value
       passError.concat("allowable")
     }

Sample use case:

  1. User edits in password parameter maintenance module the field Allowable Special Characters
  2. User defined these as allowable: ~@#$%^&*()_+{}[]:;<>,.?
  3. Now, on my New Password field, the only allowable special characters are the ff: ~@#$%^&*()_+{}[]:;<>,.?
  4. Can I achieve this requirement using regex? Note that the value ~@#$%^&*()_+{}[]:;<>,.? may change
iamjpcbau
  • 374
  • 1
  • 11
  • 29
  • 1
    What input value are you testing? That's a very specific regex, are those characters meant to be within a [character-class](https://www.regular-expressions.info/charclass.html) perhaps? – Phil May 12 '20 at 01:58
  • This regexp is kind of invalid. First you want the string to end with `#` but also contain `%` after the ending - which kind of defeats the definition of end of string. Then after the string ends you want it to start with `&` but how can a string start after it ends? – slebetman May 12 '20 at 02:03
  • Sorry this regex is meant to be a list of allowable characters. I get this from a JSON response – iamjpcbau May 12 '20 at 02:05
  • 1
    For us to help you, you have to do TWO things. 1) Define what the objective is for your regex in words. 2) Show us one or more sample inputs that you think should match and define what they should match. Right now you have a regex that would only match a very bizarre sequence of special characters. It seems unlikely that's what you want. So, you need to describe WHAT you want this to do and then we can help you better. – jfriend00 May 12 '20 at 02:10
  • I see. Sorry I didn't know what some of these sequence may look very bizarre in regex. Will edit. Thanks! – iamjpcbau May 12 '20 at 02:14

1 Answers1

1

Make a charset for the characters in your regex:

/[~@#$%^&*()_+{}[]:;<>,.?\/]/g

This will match any of the characters - and you can make it work with your current code like this:

new RegExp("[" + passParamStateMap.value + "]", "g");

Note that when you parse passParamStateMap.value, you may need to escape the special characters like [ and ], as the ] will end the character class prematurely. Here's a function from here that'll let you do just that:

new RegExp("[" + passParamStateMap.value.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + "]", "g");
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • May need to escape the `]` otherwise the character class close too early – slebetman May 12 '20 at 02:04
  • Tried the code above and when I use console.log it outputs `/[~@#$%^&*()_+{}[]:;<>,.?\/]/g` however, when I compare to `e.target.value` I still get `null` value – iamjpcbau May 12 '20 at 02:08
  • Apply the sanitisation code at the bottom @iamjpcbau – Jack Bashford May 12 '20 at 02:08
  • Thanks! I think its working now. But I have a question, what if the regex suddenly changes. Since my regex is from a variable, its very dynamic. e.g. user removed `@` and `#` it became `/[~$%^&*()_+{}[]:;<>,.?\/]/g` what changes should I do to make the regex adapt? Sorry for my english – iamjpcbau May 12 '20 at 02:12
  • 1
    The code at the bottom (`new RegExp("[" + passParamStateMap.value.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + "]", "g");`) will dynamically take the value in `passParamStateMap.value`, sanitise it, and make a global regex matching any character from it - so this will solve your problem, just put the string in `passParamStateMap.value`, or pass the value into a function. – Jack Bashford May 12 '20 at 02:13