Solved

JavaScript/REGEX: Match for URL

Posted on 2013-05-29
2
482 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 250 total points
Comment Utility
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 42

Accepted Solution

by:
David S. earned 250 total points
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now