Check Array Entries with Regex

I have an Array with one or more entries. Each one is a string (List of urls in open Tabs via Firefox SDK). I want to check if a specific url is already opened in some of the tabs (nothing special till now).

My problem is, that the url in tab list can have four diffrent fourms. For example:

Url I want to find in the tablist:

https://cmsr-author.de/cf#/content/test/de.html

But the url can also look like this:

https://cmsr-author.de/content/test/de.html
https://cmsr-author.de/test/de.html
https://cmsr-author.de/cf#/test/de.html

Of course the last part of the url (after /test/...) is always something diffrent. If I wasn't able to find one of the four urls in the tablist i want to call some other action.

My Solution till now is to build some if-chain:

if (res !== url1) {
  if (res !== url2) {
    if ...

But i thought there must be some more elegant way. Maybe via RegEx? I already have a capture to catch the first part (which stays the same https://cmsr-author.ws...) with it four forms. But i dont know how to implent this probably.

Answers


var urls = ["https://cmsr-author.de/content/test/de.html","https://cmsr-author.de/test/de.html","https://cmsr-author.de/cf#/test/de.html"]
var filtered = urls.filter(function(url) 
{ 
       return url.indexOf("cf#") > -1 && url.endsWith("/test/de.html")
})

var contains = filtered.length > 0
console.log(contains)

If you want to use regex you can do this by using groups for the middle part, which is explained in detail here: http://www.regular-expressions.info/refcapture.html

Practically, your regex would look something like that:

https:\/\/cmsr-author\.de\/(content|...|...)\/de\.html

Where ... must be replaced by the middle parts of the url which differ. Note that | is "or" used to provide multiple possibilities within the group. The character / and . must be escaped since they have special roles in regex.

I hope that helps!


My English is not good,Do not fully understand what you mean,According to my idea,You should need a regular expression,Only to match the first.If I am wrong, please @ me.

I hope that helps!

var reg = /^https:\/\/cmsr\-author\.de\/cf#\/(?:\w+\/)+test\/de\.html$/gi;
var str1 = "https://cmsr-author.de/cf#/content/test/de.html";
var str2 = "https://cmsr-author.de/content/test/de.html";
var str3 = "https://cmsr-author.de/test/de.html";
var str4 = "https://cmsr-author.de/cf#/test/de.html";
console.log(reg.test(str1));
console.log(reg.test(str2));
console.log(reg.test(str3));
console.log(reg.test(str4));

Need Your Help

How to scan QR Code in Windows Phone 8.1 using Phonegap/Cordova/WinJS/HTML/CSS/JS?

windows cordova windows-phone-8 windows-phone-8.1 zxing

I am using ZXing.Net library to scan/read QR codes from camera in Windows Phone 8.1. I went through this sample: https://github.com/wildabeast/BarcodeScanner. But this is for Windows Phone 8.0

Javascript event when mouse leaves browser window

javascript ajax safari prototypejs mouseout

I'd like some Javascript code to run when the mouse leaves the browser window. I only need to support Safari (WebKit.)