Nani
Nani

Reputation: 446

How to compare two strings in js

var url1 = 'dominName/profilePics/'
var url2 = 'dominName/profilePics/12345.jpg'

I want to compare above two urls. If any string comes after /profilePics/ it should returns true value.

I using regular expression(RegExp) but i unable to get solution.

So could you help me out of this.

Upvotes: 1

Views: 756

Answers (5)

Tarun Khurana
Tarun Khurana

Reputation: 1037

var url1 = 'dominName/profilePics/';
var url2 = 'dominName/profilePics/12345.jpg';
/dominName\/profilePics\/[0-9]+\.jpg$/.test(url1);
/dominName\/profilePics\/[0-9]+\.jpg$/.test(url2);

Upvotes: 1

hong4rc
hong4rc

Reputation: 4113

Create a regex pattern with RegExp with this string:

var myReg = new RegExp('^' + url1 + '.+');
  • ^: matches beginning of input
  • .+: matches anything (length > 0)

var url1 = 'dominName/profilePics/';
var url2 = 'dominName/profilePics/12345.jpg';
var escapeReg = url1.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
var myReg = new RegExp('^' + escapeReg + '.+');

console.log(myReg.test(url1));      //false
console.log(myReg.test(url2));      //true

Upvotes: 1

Shasak
Shasak

Reputation: 820

If I understand your problem correctly in a more general sense, it is to find whether a path is nested in another path (is a subtree). This IMO would be the right way to approach it as you don't want your solution to be specific to the url strings provided, as they can change in the future.

Proposed solutions:

Non-regex solution: Ensure url1 starts with given base url (url2) and check if after splitting on '/' the number of sub paths of url1 is greater than those of base url.

const isNested = (url1, url2) => {
  return url1.indexOf(url2) === 0 && 
  url1.split("/").length > url2.split("/").length
}

> isNested('a/b/c', 'a/b')
true
> isNested('a/b/cd', 'a/b')
true
> isNested('x/b/cd', 'a/b')
false
> isNested('a/b/cd', 'x/a/b')
false
> isNested('a/b/cd/z', 'x/a/b')
false
> isNested('a/b/cd/z', 'a/b')
true

we can add a depth parameter to test for specific depth

const isNested = (url1, url2, depth) => {
    url1.indexOf(url2) === 0 && depth
      ? url1.split("/").length - url2.split("/").length === depth
      : url1.split("/").length > url2.split("/").length
  }

> isNested('a/b/cd/z', 'a/b')
true
> isNested('a/b/cd/z', 'a/b', 1)
false
> isNested('a/b/cd/z', 'a/b', 2)
true

Regex solution: Not recommended as it is difficult to read and modify.

const isNested = (url1, url2) => {
  let escaped = url2.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&")

  let regex = new RegExp(`(^${escaped})(\/[\/a-zA-Z0-9_-]+)`, "gm")

  return url1.match(regex) ? true : false
}


> isNested('a/b/c/d', 'a/b/c')
true
> isNested('a/b/c', 'a/b/c')
false
> isNested('a/b/c/de', 'a/b/c')
true
> isNested('a/b/c/de/fg', 'a/b/c')
true
> isNested('x/a/b/c/de/fg', 'a/b/c')
false

Upvotes: 0

Ivan
Ivan

Reputation: 40668

You could use the .substring method to get the length of the string portion after the domain name:

const checkURL = url => {
  
  return Boolean(url.substring('dominName/profilePics/'.length));

}

console.log(
  checkURL('dominName/profilePics/')           // false
)

console.log(
  checkURL('dominName/profilePics/12345.jpg')  // true
)

Upvotes: 0

Monica Acha
Monica Acha

Reputation: 1086

const checkURL = (url) => {
  
  return Boolean(url.substring(url.lastIndexOf('/')+1));

}

console.log(
  checkURL('dominName/profilePics/')           // false
)

console.log(
  checkURL('dominName/profilePics/12345.jpg')  // true
)

Upvotes: -1

Related Questions