简体   繁体   English

Javascript正则表达式冒号?

[英]Javascript Regex for colon?

I'm wondering how I can use javascript regex to match a word like: 我想知道如何使用javascript正则表达式匹配一个单词,如:

Name:

with all variations of spaces ? 有各种空间变化? This is where I'm getting stuck. 这是我陷入困境的地方。 ie

  • Name<space>: Fred
  • Name:<space>Fred or Name<space>:<space>Fred Name:<space>FredName<space>:<space>Fred

Note the positioning of the spaces after the name, after the colon etc ? 注意名称后面的空格位置,冒号后面等等?

I was hoping something like /(name(\\s*:\\s*)?)\\w/g would work but it doesn't :( 我希望像/(name(\\s*:\\s*)?)\\w/g可以工作,但它不会:(

  • Name starts with a capital letter. 名称以大写字母开头。 The regex should also match name starting with a capital N. 正则表达式还应匹配以大写字母N开头的名称。

    • If you want the entire regex to be case insensitive, add the i flag at the end. 如果您希望整个正则表达式不区分大小写,请在结尾添加i标志。
    • If you only want name to start with a lower or upper case N, then use a set 如果您只希望名称以小写或大写N开头,则使用集合
  • The * means 0 or more. *表示0或更多。 The ? ? is not needed anymore. 不再需要了。

Something like this should work 这样的事情应该有效

/Name\s*:\s*\w*/g    //matches "Name"

/[Nn]ame\s*:\s*\w*/g //matches "Name" or "name"

/name\s*:\s*\w*/gi   //the entire regex is case insensitive

正则表达式/name\\s*:\\s*\\w/gi就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript正则表达式按单词或冒号拆分 - JavaScript regex split on word or colon Javascript正则表达式与Twitter用户名匹配,但排除冒号 - Javascript regex match Twitter username, but exclude colon 带分号和空格的JavaScript正则表达式 - JavaScript Regex with semi-colon and whitespace Javascript regex - 删除除分号以外的所有特殊字符 - Javascript regex - remove all special characters except semi colon Javascript正则表达式可在第一个冒号和行尾或第二个冒号之间选择文本 - Javascript Regex to select text between first colon and optionally either end of line or a second colon 正则表达式替换JS中的冒号 - Regex to replace colon in JS 句点、单词和冒号的正则表达式 - Regex for period, word, and then colon 如何使用正则表达式在javascript中查找和替换所有属性,并在无冒号的情况下以大括号开头 - How to find and replace all properties proceeding with open brace with no colon in javascript using regex 如何从字符串(JavaScript正则表达式)中提取冒号分隔的键值和可选出现的值? - How can I extract colon separated key values with optional occurances from a string (Javascript regex)? Javascript 正则表达式允许数字和特殊字符(如点(.)和冒号(:))验证特定主机名 - Javascript regex to allow numbers and special characters like dot(.) and colon (:) for validating specific hostnames
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM