Solved

JavaScript/REGEX: Match for URL

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Suggested Courses

627 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