0

I would like to write an IF condition that contains an exact match of letters found in a link, but the way I wrote the if, probably isn't the best method.

Can anyone help me understand better?

The condition to find the letters in the link is written below, but I think it would be better to use a RegEx. How do I write the condition in the if, to contain the RegEx?

  const findUrl = "http://wwww.example.com/dk"; 

The IF condition is written like this for now and uses only the findUrl variable. How can I write it to use the RegEx for exact matching of dk letters?

if(findUrl.includes('dk')) {
     // some code here
    }
onweb
  • 75
  • 7
  • So, either `/` or end of string after these two letters, right? `/\.com\/(\w{2})(?:\/|$)/`? – Wiktor Stribiżew Mar 05 '21 at 14:57
  • 1
    After you've matched the regex `const match = regex.exec(findUrl)` or `const match = findUrl.match(regex)` (the latter only without the `g` flag), you can then use the if-statement `if (match && match[1] == "dk")`. – 3limin4t0r Mar 05 '21 at 15:26
  • @3limin4t0r Thanks for your answer! Unfortunately my answer was rated with -1 but thanks to you it still helped me a lot on my little project – onweb Mar 06 '21 at 16:44

0 Answers0