1
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.

6
  • 1
    /dominName\/profilePics\/.*$/i.test(url2) Commented Jul 8, 2018 at 13:27
  • hi.. @Olian04 thx for your answer. could you pls elaborate your answer Commented Jul 8, 2018 at 13:59
  • @Nani its just a regex. If you click on my previous comment you can see a demo of it working. Commented Jul 8, 2018 at 14:04
  • 1
    thnx @Olian04 it is working fine Commented Jul 8, 2018 at 14:13
  • 1
    I would recommend you change the title of the question to: "How to check if a url path is a subtree of another url path." You would want a more general solution as the urls to match might not always be known, or could change in the future. Commented Jul 8, 2018 at 15:36

5 Answers 5

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

You would want to escape the inserted string.
1
var url1 = 'dominName/profilePics/';
var url2 = 'dominName/profilePics/12345.jpg';
/dominName\/profilePics\/[0-9]+\.jpg$/.test(url1);
/dominName\/profilePics\/[0-9]+\.jpg$/.test(url2);

1 Comment

[0-9]+ will be changed if there is possibility of alphabets character i.e. [0-9,a-z,A-Z]. I have assumes ".jpg" will be there if something come.
0

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
)

4 Comments

hi... @Ivan is there anyway to get solution by using regEx
@Nani check my answer!
this will fail the following use cases > checkURL('bla/dominName/profilePics/123') true > checkURL('bla/dominName/profilePics') true it also assumes the url pattern is already know. This is not a good way to approach the problem as even slight change in url would require changing the code. Check my answer for proper approach.
This code check length of url > length of 'dominName/profilePics/'
0

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

Comments

-1

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

}

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.