Link to home
Start Free TrialLog in
Avatar of MJ
MJFlag for United States of America

asked on

Regx for 2 terms?

I want to detect if a variable contains either "branch-result" or "App/Locator". What would be the best approach?

Thanks!
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

try index of function like below -

var sentence="Hi branch-result I am pawan"
if (sentence.indexOf("branch-result")!=-1)
alert("yes got branch-result")

if (sentence.indexOf("App/Locator")!=-1)
alert("yes got App/Locator")

you can also look at the other javascript string functions.
https://www.sitepoint.com/15-javascript-string-functions/
Avatar of MJ

ASKER

I understand that but I thought a single line regx would be more eficient?
I dont think you need regex for this kind of thing. Regex is more of a like search. in this case we know the final strings we are searching.
Avatar of ozo
branch-result|App/Locator
Whether or not it is more efficient may depend on details of your implementation and application
Avatar of MJ

ASKER

It just looks like this could be a lot more efficient?
return (function() { 
    "use strict";
    var currQS = '';
    try {
        if((window.location.href.indexOf("branch-result")!==-1)||(window.location.href.indexOf("App/Locator")!==-1)) {
            var regex = new RegExp('[\\?&]address=([^&#]*)');
            currQS = regex.exec(location.search);
            return currQS === null ? '' : decodeURIComponent(currQS[1].replace(/\+/g, ' '));   
        }
    }catch(e) {}
	return currQS;
})();

Open in new window

Avatar of MJ

ASKER

What about:
if (window.location.href.match(/(branch-result|App\/Locator))) 

Open in new window

Avatar of MJ

ASKER

So in this case, which would be more efficient as far as performance?
if((window.location.href.indexOf("branch-result")!==-1)||(window.location.href.indexOf("App/Locator")!==-1)) {

Open in new window


 or this approach:
if (window.location.href.match(/(branch-result|App\/Locator)/g)){

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial