Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

JavaScript/REGEX: Match for URL

Posted on 2013-05-29
2
Medium Priority
?
513 Views
Last Modified: 2013-05-29
How can I get a match for this?

var str = 'https://www.example.com/cart.html?Product_Code1=&Quantity1=&';
var zR=/^https:\/\/www.example.com\/cart.html?Product_Code1=&Quantity1=&/i;
if(zR.test(str)) {
 alert('Match Found');
}

Open in new window

0
Comment
Question by:hankknight
2 Comments
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 1000 total points
ID: 39204042
You don't need a regex for this : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf

if( str.indexOf(url) >= 0 ) {
 alert('Match Found');
}
0
 
LVL 43

Accepted Solution

by:
David S. earned 1000 total points
ID: 39204067
The reason your expression was not working is that the question mark is a special character in regex (it indicates the previous character or group is optional), so you need to escape it like the (forward-)slashes.

var zR=/^https:\/\/www.example.com\/cart.html\?Product_Code1=&Quantity1=&/i;

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question