Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JavaScript/REGEX: Match for URL

Posted on 2013-05-29
2
Medium Priority
?
507 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

722 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