简体   繁体   English

Javascript正则表达式只匹配以特定特殊字符开头的单词

[英]Javascript regex match only words starting with a specific special character

I'm trying to match only words starting with # in javascript, for eg. 我试图在javascript中仅匹配以#开头的单词,例如。 in the following sample text, only #these should match. 在以下示例文本中,只有#these应该匹配。

I need to match only words like #these. 我只需匹配像#these这样的单词。 Ignore the ones like @#this , !#this and in#ignore. 忽略像@#this,!#this和#ignore中的那些。

The closer I got is here, 我越接近这里,

/(\B(#[a-z0-9])\w+)/gi

Ref: https://regex101.com/r/wU7sQ0/114 参考: https//regex101.com/r/wU7sQ0/114

Use a whitespace boundary (?:^|\\s) : 使用空白边界(?:^|\\s)

 var rx = /(?:^|\\s)(#[a-z0-9]\\w*)/gi; var s = "I need to match only words like #these. \\nIgnore the ones like @#this , !#this and in#ignore."; var m, res=[]; while (m = rx.exec(s)) { res.push(m[1]); } console.log(res); 

Details : 细节

  • (?:^|\\s) - matches the start of string or whitespace (?:^|\\s) - 匹配字符串或空格的开头
  • (#[a-z0-9]\\w*) - Group 1 ( m[1] ): a # , then an alphanumeric char followed with 0 or more word chars (letters, digits, _ symbols). (#[a-z0-9]\\w*) - 组1( m[1] ): # ,然后是一个字母数字字符,后跟0个或多个字符(字母,数字, _符号)。

See the regex demo , pay attention to what texts are captured , rather to the whole matches. 查看正则表达式演示 ,注意捕获的文本,而不是整个匹配。

Or trimming each match: 或修剪每场比赛:

 var rx = /(?:^|\\s)(#[a-z0-9]\\w*)/gi; var s = "I need to match only words like #these. \\nIgnore the ones like @#this , !#this and in#ignore."; var results = s.match(rx).map(function(x) {return x.trim();}); // ES5 // var results = s.match(rx).map(x => x.trim()); // ES6 console.log(results); 

Why not search start or with space before. 为什么不先搜索开始或有空格。 see regex / #|^# 看到正则表达式 / #|^#

I little bit changed your regex to get what do you want. 我有点改变你的正则表达式来得到你想要的东西。

var p = "I need to match only words like #these. Ignore the ones like @#this , !#this and in#ignore."

[^@!a-z$]\#[a-z]+

That will be match only #these, you can exclude whatever you don't want by adding between first square bracket 这将只匹配#these,您可以通过在第一个方括号之间添加来排除您不想要的任何内容

you can try this, 你可以尝试这个,

 txt = "I need to match only words like #these. Ignore the ones like @#this , !#this and in#ignore." console.log(/(^|\\s)(#+\\w+)/g.exec(txt)) 

暂无
暂无

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

相关问题 Javascript:提取字符串中以特定字符开头的单词 - Javascript : Extract words starting with specific character in a string javascript正则表达式以允许数字和特定的特殊字符 - javascript regex to allow numbers and specific special character 正则表达式只匹配带有特殊字符的长字符串中的单词 - Regex to match words only in long string with special characters RegEx仅匹配前后没有特定单词的单词 - RegEx match only words with no specific words before and after JavaScript正则表达式可匹配2个单词和一个长度受限制的空白字符 - JavaScript Regex to match 2 words and an whitespace character with length limitations JavaScript中的RegEx仅屏蔽字符串中的数字和特殊字符(/,空格, - ) - RegEx in JavaScript to mask only digit and special character(/, space, -) in a string 正则表达式匹配仅包含特定字符的整个单词 - Regex to match entire words containing only specific characters 正则表达式仅在 JavaScript 中存在字母或数值时才接受特殊字符? - Regex to accept special character only in presence of alphabets or numeric value in JavaScript? 匹配不以特殊字符开头的行中的换行符 - Match line breaks in lines that are not starting with a special character Javascript正则表达式匹配至少一个以数字开头的单词跨越多个单词但至少有两个单词 - Javascript regex to match atleast one word starting with number across multiple words but with atleast two words
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM