Skip to content

Instantly share code, notes, and snippets.

@mrboli
Created April 11, 2023 16:38
Show Gist options
  • Save mrboli/c3688fb0a5b40b2f084aaab148e26a46 to your computer and use it in GitHub Desktop.
Save mrboli/c3688fb0a5b40b2f084aaab148e26a46 to your computer and use it in GitHub Desktop.
/\(?\d{3}\)?\s*-?\s*\d{3}\s*-?\s*\d{4}/.test()
false
const testValue = 'sdfsdf425 - 319 - 5483'
undefined
/\(?\d{3}\)?\s*-?\s*\d{3}\s*-?\s*\d{4}/.test(testValue)
true
testValue.match(/\(?\d{3}\)?\s*-?\s*\d{3}\s*-?\s*\d{4}/)
['425 - 319 - 5483', index: 6, input: 'sdfsdf425 - 319 - 5483', groups: undefined]0: "425 - 319 - 5483"groups: undefinedindex: 6input: "sdfsdf425 - 319 - 5483"length: 1[[Prototype]]: Array(0)
testValue.match(/\(?\d{3}\)?\s*-?\s*\d{3}\s*-?\s*\d{4}/)[0].match(/\d/)
['4', index: 0, input: '425 - 319 - 5483', groups: undefined]0: "4"groups: undefinedindex: 0input: "425 - 319 - 5483"length: 1[[Prototype]]: Array(0)
testValue.match(/\(?\d{3}\)?\s*-?\s*\d{3}\s*-?\s*\d{4}/)[0].match(/\d/g)
(10) ['4', '2', '5', '3', '1', '9', '5', '4', '8', '3']0: "4"1: "2"2: "5"3: "3"4: "1"5: "9"6: "5"7: "4"8: "8"9: "3"length: 10[[Prototype]]: Array(0)
testValue.match(/\(?\d{3}\)?\s*-?\s*\d{3}\s*-?\s*\d{4}/)[0].match(/\d/g).join('')
'4253195483'
testValue.match(/\(?\d{3}\)?\s*-?\s*\d{3}\s*-?\s*\d{4}/)[0].match(/\d/g).join('')
'4253195483'
const phoneNumberRegex = new RegExp('\(?\d{3}\)?\s*-?\s*\d{3}\s*-?\s*\d{4}')
VM406:1 Uncaught SyntaxError: Invalid regular expression: /(?d{3})?s*-?s*d{3}s*-?s*d{4}/: Invalid group
at new RegExp (<anonymous>)
at <anonymous>:1:26
(anonymous) @ VM406:1
const phoneNumberRegex = new RegExp(\(?\d{3}\)?\s*-?\s*\d{3}\s*-?\s*\d{4})
VM418:1 Uncaught SyntaxError: Invalid or unexpected token
const phoneNumberRegex = new RegExp(/\(?\d{3}\)?\s*-?\s*\d{3}\s*-?\s*\d{4}/)
undefined
testValue.match(phoneNumberRegex)
['425 - 319 - 5483', index: 6, input: 'sdfsdf425 - 319 - 5483', groups: undefined]
phoneNumberRegex.test(testValue)
true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment