0

Like tried with this sample with no special character:

let x = new RegExp('^[A-Za-z\s0-9]+$'); // with no special char

x.test('Hello world'); // should return true
x.test('Hello world with something'); // should return true

x.test(' Hello world'); // should return false
x.test('Hello world '); // should return false
x.test('Hello  world'); // should return false
  • `const x = /^[a-zA-Z0-9]+(?:\s[a-zA-Z0-9]+)*$/` – Wiktor Stribiżew Jan 25 '22 at 21:04
  • you need to escape the backslash since you're using quotes --- `'^[A-Za-z\\s0-9]+$'` – skara9 Jan 25 '22 at 22:14
  • for regex you should use either a regex literal: `let x = /^[A-Za-z\s0-9]+$/` or a raw string: `let x = new RegExp(String.raw\`^[A-Za-z\s0-9]+$\`)` – skara9 Jan 25 '22 at 22:17
  • @WiktorStribiżew that link is not exactly duplicate of this question. Some of them almost the answer but with another propose. As i google it, there is no good pattern that follow all characters, so i made this: https://regexr.com/6e1o4 for question owner. Please reopen it to i can explain it. – MMMahdy-PAPION Jan 25 '22 at 23:06
  • 1
    @MMMahdy-PAPION It is a valid dupe reason. However, even for your reading of this question, there is [another dupe reason](https://stackoverflow.com/questions/34974942/) with several matching answers. – Wiktor Stribiżew Jan 25 '22 at 23:09
  • @WiktorStribiżew, Thank you. This new link you add made it right. – MMMahdy-PAPION Jan 25 '22 at 23:12
  • _"no leading and trailing space only middle space"_ who cares about the middle ? This `^(?!\s)(?!.*\s$).*` fairly simple. What is the Duplicate about ? – sln Jan 25 '22 at 23:50
  • https://stackoverflow.com/a/70857303/7514010 – MMMahdy-PAPION Jan 26 '22 at 00:13

0 Answers0