0

I have a string from which I want to check if it only contains characters that are allowed. I want only characters of

  • alphanumeric (lower & upper) : A-Z, a-z

  • numeric (0-9)

  • special characters: _&!.-

  • space

example of valid strings:

  • title1 - header
  • title2&&& header_!.
  • 09
  • a912
  • ...dd

example of invalid strings

  • a$$$ ()
  • a12345^&6**

if at least one of characters is not in the special characters so it not valid string. I would like to know how do test it in javascript. the regex patterns come from server so it string. I convert the regex pattern string by using RegExp but all of them show false, even some of them are valid. the three first lines should be valid (true)

 const pattern = "^[a-z0-9_&!.-\\s]+$";

                const strings = [
                  `title1 - header`,
                  `title2&&& header_!.-`,
                  `09`,
                  `a912 dd`,
                  `a$$$ ()`,
                  `a12345^&6**`
                ]
                const regex = new RegExp(pattern);

                strings.forEach(str => {

                  console.log(str, ' ---> ', regex.test(str));
                });
Manspof
  • 598
  • 26
  • 81
  • 173
  • 1
    `"/^[a-z0-9_&!.-\s]+$/"` is not valid input for the RegExp constructor because you shouldn't be passing the `/` delimiters there. Either change your backend to give you valid regular expressions or pre-process this before feeding it into RegExp – VLAZ Jul 04 '22 at 14:48
  • how to improve it? to remove / delimiter? – Manspof Jul 04 '22 at 14:51
  • [How to remove the first and the last character of a string](https://stackoverflow.com/q/20196088) – VLAZ Jul 04 '22 at 14:53
  • no, I mean what do do that the regex pattern will work as expected? I'm the backend developer too, I sent it from server, so I can replace any value – Manspof Jul 04 '22 at 14:55
  • Then don't send the `/` at the beginning and the end. – VLAZ Jul 04 '22 at 14:55
  • I edited the post and change it but still not valid – Manspof Jul 04 '22 at 14:57
  • 1
    1. You only changed the indentation 2. Your `pattern` variable is wrong here - `\s` has to be double escaped as `\\s`. But that's only an issue with the stack snippet. [Why do regex constructors need to be double escaped?](https://stackoverflow.com/q/17863066) – VLAZ Jul 04 '22 at 14:58
  • check my edit post.. maybe my regex it not valid, if you think I need to change it ok, I wrote the rules in the post (A-Za-z0-9 ._&!.-) – Manspof Jul 04 '22 at 15:04
  • You deleted the `-` which was part of your pattern – VLAZ Jul 04 '22 at 15:11
  • right, that's working. what is mean "\\s"? why I need it? and why I need "$"? – Manspof Jul 04 '22 at 15:15
  • 1
    [Why do regex constructors need to be double escaped?](https://stackoverflow.com/q/17863066) | [Matching exact string with JavaScript](https://stackoverflow.com/q/447250) | [What do ^ and $ mean in a regular expression?](https://stackoverflow.com/q/6908725) | [Reference - What does this regex mean?](https://stackoverflow.com/q/22937618) – VLAZ Jul 04 '22 at 15:21

0 Answers0