Solved

JavaScript/REGEX: Match for URL

Posted on 2013-05-29
2
486 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
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 42

Accepted Solution

by:
David S. earned 250 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Lazy load not working with masonry js 1 15
REXEX help Part 2 2 22
How to create a textarea which saves text in HTML 8 35
Hide cell in a table 2 9
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

786 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