简体   繁体   中英

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. i have managed to write a function to match linkedin in the string, but its taking both cases ie linkedin and linkedinpage. Please help me to find the regular expression to match linkedin only. 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 . You can just use:

/linkedin\b/.test(str);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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