Home > Software engineering >  How to implement a function that matches a specific paragraph in JavaScript?
How to implement a function that matches a specific paragraph in JavaScript?

Time:05-12

full url : abc.com/product/123

find url : abc.com/product

I want to return true if the full url contains find url

The condition must match the sentences of find url and the words in the full url perfectly.

fullurl.indexOf('abc') // this find true It is a condition that only certain words should not be split.

I want the result value

fullurl = 'abc.com/product/123'; 
findurl = 'abc.com/product';

fullurl.indexOf('abc'); // this find false 
fullurl.indexOf('pro'); // this find false 
fullurl.indexOf('abc.com'); // this find false 
fullurl.indexOf('abc.com/product'); // this true This result is the return value I want

I can't get the result I want with the indexOf function alone. What should I do?

CodePudding user response:

This will return true if fullurl contains findurl.

I used regex, if you want to change the findurl you need to edit the regex code.

function matchUrl(fullurl) {
  reg = /\babc.com\/product\b/;
  if (fullurl.match(reg)) {
    return true;
  }
  return false;
}

Check this out to further understand the regex I used.

  • Related