3

I'd like to quickly check if a string is valid to be used as a property name using the dot notation rules (any letters or numbers as well as _and $ as long as it doesn't start with a number) as obviously if bracket notation is used then everything is valid.

I've been trying to figure out a regEx solution but my knowledge of regEx is not great. I think that my current pattern will allow letters, numbers, $ and _ but I don't know how to disallow starting with a number

function validName(str){
    // check if str meets the requirements 
    return /^[a-zA-Z0-9$_]+$/.test(str);
}

validName("newName")    // should return TRUE
validName("newName32")  // should return TRUE
validName("_newName")   // should return TRUE
validName("4newName")   // should return FALSE
validName("new Name")   // should return FALSE
validName("")           // should return FALSE
tyler mackenzie
  • 622
  • 7
  • 18
  • 2
    You might find [regexr.com](https://regexr.com/) or [regex101.com](https://regex101.com) useful. – showdev Feb 27 '19 at 01:25

4 Answers4

5

Since \w covers [a-zA-Z0-9_] and \d covers [0-9] you could use this regex:

const validName = str => /^(?!\d)[\w$]+$/.test(str);

console.log(validName("newName")) // should return TRUE
console.log(validName("newName32")) // should return TRUE
console.log(validName("_newName")) // should return TRUE
console.log(validName("4newName")) // should return FALSE
console.log(validName("new Name")) // should return FALSE
console.log(validName("")) // should return FALSE
Scott Rudiger
  • 1,224
  • 12
  • 16
3

Adding a negative lookahead should be good enough.

^(?![0-9])[a-zA-Z0-9$_]+$

Test

function validName(str) {
  // check if str meets the requirements 
  return /^(?![0-9])[a-zA-Z0-9$_]+$/.test(str);
}

console.log(validName("newName")) // should return TRUE
console.log(validName("newName32")) // should return TRUE
console.log(validName("_newName")) // should return TRUE
console.log(validName("4newName")) // should return FALSE
console.log(validName("new Name")) // should return FALSE
console.log(validName("")) // should return FALSE
kerwei
  • 1,822
  • 1
  • 13
  • 22
2

You can just make the first character of the pattern the same character set, except without including numbers:

^[a-zA-Z$_][a-zA-Z0-9$_]*$

CAustin
  • 4,525
  • 13
  • 25
0

When solving regex like this I recommend using regexr.com

This snippet should take care of your issue.

function validName(str){
    // check if str meets the requirements
    return /^[^0-9][a-zA-Z0-9$_]+$/.test(str)
}

console.log(validName("newName"))   // TRUE
console.log(validName("newName32")) // TRUE
console.log(validName("_newName"))  // TRUE
console.log(validName("4newName"))  // FALSE
console.log(validName("new Name"))  // FALSE
console.log(validName(""))          // FALSE
John Vandivier
  • 2,158
  • 1
  • 17
  • 23
  • Incidentally, this will match a valid name that is preceded by a space (although it's not in the OP's test cases). For example, " newName32". – showdev Feb 27 '19 at 01:40
  • what is the advantage of not using a lookaround? – tyler mackenzie Feb 27 '19 at 01:45
  • It's true, `[^0-9]` is explicitly checking "doesn't start with a number" as OP said. Another interpretation would be "can start with any of the chars allowed elsewhere, except a number" which would be accomplished like `/^[^a-zA-Z$_][a-zA-Z0-9$_]+$/` – John Vandivier Feb 27 '19 at 01:50
  • 1
    Using a lookaround in modern JS is fine; in fact [jsben](http://jsben.ch/Jz8FL) says [@kerwei's answer](https://stackoverflow.com/a/54896591/3931488) is about 13% faster in Chrome 72 which blows my mind a bit – John Vandivier Feb 27 '19 at 01:53
  • 1
    Back in the day, and in some apps/languages even today, lookarounds just weren't supported. That's one reason not to use them depending on what you need to support. Another reason is readability. Again, nbd if they're intuitive to you and your team, but I find them sometimes hard to comprehend. – John Vandivier Feb 27 '19 at 01:54
  • Ah ok that makes sense! very good thing to keep in mind as I learn more about regex. – tyler mackenzie Feb 27 '19 at 02:14
  • This answer is actually wrong, allowing ANY character in the first digit that isn't a number, including wild unicode characters, spaces, just about anything. – ErikE Feb 10 '23 at 23:39