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 
8
  • const x = /^[a-zA-Z0-9]+(?:\s[a-zA-Z0-9]+)*$/ Commented Jan 25, 2022 at 21:04
  • you need to escape the backslash since you're using quotes --- '^[A-Za-z\\s0-9]+$' Commented Jan 25, 2022 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]+$`) Commented Jan 25, 2022 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: regexr.com/6e1o4 for question owner. Please reopen it to i can explain it. Commented Jan 25, 2022 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 with several matching answers. Commented Jan 25, 2022 at 23:09

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.