1

I have this code:

CODE JS:

const phone= /^\(?(\d{3})\)?[-]?(\d{3})[-]?(\d{4})$/;

After running this code has the following form

(123)132-1312

I want to add white space before the last parentheses as in the example below.

(123 )132-1312

Can you please tell me how to accept changes to the code so that white space?

Thanks in advance!

EDIT:

text = $(this).val().replace(/(\d{3})(\d{3})(\d{4})/, "($1)$2-$3");
var testt=$(this).val().match(text);

I added this code to call here as add white space ...

Marius
  • 1,181
  • 3
  • 15
  • 23

2 Answers2

1

This should work as you need:

/^\(?\s*(\d{3})\s*\)?[-]?(\d{3})[-]?(\d{4})$/
vard
  • 4,057
  • 2
  • 26
  • 46
Ram
  • 130
  • 9
1

Depending on wether you always want the whitespace befor the closeing parenthesis or not your could go with:

^\(?(\d{3}\s?)\)?[-]?(\d{3})[-]?(\d{4})$

Background \s is for whitespaces. the ? stands for 0 or 1 occurrance of the previous sign. if you use + it is one or more and * is for 0 or more occurrences of the previous sign.

To test your regex you can always use regexpal

Fuujin
  • 161
  • 1
  • 6