0

I would like to do a regular expression that does not allow more than 1 consecutive spaces.

For example:

  • A Bb 7 is valid
  • AA O is not valid
  • 00 55 is valid
  • A b is not valid
2

3 Answers 3

0

Here you can try this logic :

let str = "apple mango pine";

let result = str.match(/ {2,}/g);

if (result) {
  console.log("two consecutive spaces are not allowed");
} else console.log("valid");

Sign up to request clarification or add additional context in comments.

Comments

0

Depending on your exact needs I can provide two versions:

This works with only the "space" character, but does not take other spacing characters from Unicode into account:

^[^ ]*(?: [^ ]+)* ?$

That takes the regex spacing characters into account, so also uses newlines, tabs etc. as "space":

^\S*(?:\s\S+)*\s?$

Both regular expressions match when they find a valid input.

Comments

0

What makes you think you need a regular expression for this?

 if (string.includes('  '))
    alert('error!')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.