最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Validating property names with regEx - Stack Overflow

programmeradmin2浏览0评论

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

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
Share asked Feb 27, 2019 at 1:12 tyler mackenzietyler mackenzie 6729 silver badges19 bronze badges 1
  • 2 You might find regexr. or regex101. useful. – showdev Commented Feb 27, 2019 at 1:25
Add a ment  | 

4 Answers 4

Reset to default 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

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

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$_]*$

When solving regex like this I remend using regexr.

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

发布评论

评论列表(0)

  1. 暂无评论