MJ
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!
Thanks!
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.
branch-result|App/Locator
Whether or not it is more efficient may depend on details of your implementation and application
Whether or not it is more efficient may depend on details of your implementation and application
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;
})();
ASKER
What about:
if (window.location.href.match(/(branch-result|App\/Locator)))
ASKER
So in this case, which would be more efficient as far as performance?
or this approach:
if((window.location.href.indexOf("branch-result")!==-1)||(window.location.href.indexOf("App/Locator")!==-1)) {
or this approach:
if (window.location.href.match(/(branch-result|App\/Locator)/g)){
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
var sentence="Hi branch-result I am pawan"
if (sentence.indexOf("branch-
alert("yes got branch-result")
if (sentence.indexOf("App/Loc
alert("yes got App/Locator")
you can also look at the other javascript string functions.
https://www.sitepoint.com/15-javascript-string-functions/