简体   繁体   English

正则表达式匹配完全字符串

[英]Regular expression to match exact string

I have a string variable 我有一个字符串变量

var str = ad2672a2361d10eacf8a05bd1b10d4d8linkedin or ad2672a2361d10eacf8a05bd1b10d4d8linkedinpage

I want a regular expression for exact match of linkedin in this string. 我想在这个字符串中使用正则表达式来完全匹配linkedin。 i have managed to write a function to match linkedin in the string, but its taking both cases ie linkedin and linkedinpage. 我已经设法写了一个函数来匹配字符串中的linkedin,但它采取了两种情况,即linkedin和linkedinpage。 Please help me to find the regular expression to match linkedin only. 请帮我找到匹配linkedin的正则表达式。 Thanks 谢谢

this is my code 这是我的代码

if (/.*linkedin/.test(str)) {
    // found linkedin only 
}
if (/.*linkedin$/.test(str)) {
    // found linkedin only 
}

Try this 尝试这个

if (/.*linkedin$/.test(str)){
      //matches only linkedin, xxxxlinkedin,but not linkedinxxxx,xxxxlinkedinzzzz    
}

Looks like you only want word boundary to be applied after linkedin . 看起来你只想在linkedin之后应用单词边界。 You can just use: 你可以使用:

/linkedin\b/.test(str);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM