0

I have a system path URL in the form of string from which I have to find the specific keyword "System". How can I achieve this? either by regex or any other way.

var key = "System";
var url = "/System/Library/PrivateFrameworks/PhotoLibrary.framework/Versions/A/XPCServices/com.apple.PhotoIngestService.xpc/Contents/MacOS/com.apple.PhotoIngestService";

Now url has word "System". I need a function which detects the key in url. If exist it return true.

Anand Vaidya
  • 609
  • 2
  • 16
  • 41

3 Answers3

-1

No need for regex.

    var key = "System"
    var url = "/System/Library/PrivateFrameworks/PhotoLibrary.framework/Versions/A/XPCServices/com.apple.PhotoIngestService.xpc/Contents/MacOS/com.apple.PhotoIngestService"
    var exists = url.contains(key)  //returns true

    var exists2 = url.indexOf(key) !== -1   //returns true
ninesalt
  • 4,054
  • 5
  • 35
  • 75
-1

You can use indexOf(), returns the position of the string in the other string. If not found, it will return -1.

var key = "System";
var url = "/System/Library/PrivateFrameworks/PhotoLibrary.framework/Versions/A/XPCServices/com.apple.PhotoIngestService.xpc/Contents/MacOS/com.apple.PhotoIngestService";
var isExists = url.indexOf(key) !== -1;
console.log(isExists);
Sudhir Ojha
  • 3,247
  • 3
  • 14
  • 24
-1

The best way is using includes() that returns true or false (not an index), I think is more clear and more maintainable for this purpose.

But as we can see here, it is not supported by IE now. Here the Polyfill made by MDN.

About the difference between includes() and indexOf() you could read this.

And here an example:

var key = "System";
var url = "/System/Library/PrivateFrameworks/PhotoLibrary.framework/Versions/A/XPCServices/com.apple.PhotoIngestService.xpc/Contents/MacOS/com.apple.PhotoIngestService";

var isPresent = url.includes(key);

console.log(isPresent);
Emeeus
  • 5,072
  • 2
  • 25
  • 37