Dibish
Dibish

Reputation: 9303

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 
}

Upvotes: 0

Views: 322

Answers (3)

malkam
malkam

Reputation: 2355

Try this

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

Upvotes: 1

anubhava
anubhava

Reputation: 786136

Looks like you only want word boundary to be applied after linkedin. You can just use:

/linkedin\b/.test(str);

Upvotes: 0

Maurice Perry
Maurice Perry

Reputation: 32831

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

Upvotes: 1

Related Questions