-2

I have a url like so: group/wtbxqcum This is a dynamic part: wtbxqcum

How to check if something exists after group/?

I was able to do this if URL is like so: group?type=all

e.g.

 if (authReq.url.indexOf('type=all') >= 0) {
   //code
    }

P.S.

My URL is like so: authReq.url = 'Prod/v1/group/wtbxqcum';

Sampath
  • 63,341
  • 64
  • 307
  • 441

1 Answers1

1

you could achieve this the following ways:

  1. find index of "group/", then add 6 (no. of chars in 'group/') & compare it with the length.
  2. split the string at "group/" and check if the second element in the returned array isn't empty.

const str = "blah/group/dynamicStr"
const n = 6 // no of chars in "group/"
if (str.indexOf("group/") + n < str.length){
  console.log("something exists")
  // str.substr(str.indexOf("group/")+n, str.length) will return the value that exists
} else {
  console.log("nothing exists")
}

// or

const a = str.split("group/")
if (a.length == 2 && a[1] != ''){
  console.log(a[1], " exists")
}else {
  console.log("nothing exists")
}
Nick
  • 63
  • 1
  • 7