How can I match on, but exclude a regex pattern?
Asked Answered
E

6

45

I have this URL:

http://example.com/createSend/step4_1.aspx?cID=876XYZ964D293CF&snap=true&jlkj=kjhkjh&

And this regex pattern:

cID=[^&]*

Which produces this result:

cID=87B6XYZ964D293CF

How do I REMOVE the "cID="?

Thanks

Elytron answered 9/6, 2010 at 5:38 Comment(3)
what language? are you working in?Besetting
Which regex parser are you using?Bobinette
Sorry, I should have been more specific. I'm working in javascriptElytron
H
39

You can either use lookbehind (not in Javascript):

(?<=cID=)[^&]*

Or you can use grouping and grab the first group:

cID=([^&]*)
Heliopolis answered 9/6, 2010 at 5:43 Comment(0)
E
19

Generally speaking, to accomplish something like this, you have at least 3 options:

  • Use lookarounds, so you can match precisely what you want to capture
    • No lookbehind in Javascript, unfortunately
  • Use capturing group to capture specific strings
    • Near universally supported in all flavors
  • If all else fails, you can always just take a substring of the match
    • Works well if the length of the prefix/suffix to chop is a known constant

References


Examples

Given this test string:

i have 35 dogs, 16 cats and 10 elephants

These are the matches of some regex patterns:

You can also do multiple captures, for example:

  • (\d+) (cats|dogs) yields 2 match results (see on rubular.com)
    • Result 1: 35 dogs
      • Group 1 captures 35
      • Group 2 captures dogs
    • Result 2: 16 cats
      • Group 1 captures 16
      • Group 2 captures cats
Epode answered 9/6, 2010 at 7:58 Comment(0)
A
9

With JavaScript, you'll want to use a capture group (put the part you want to capture inside ()) in your regular expression

var url = 'http://example.com/createSend/step4_1.aspx?cID=876XYZ964D293CF&snap=true&jlkj=kjhkjh&';

var match = url.match(/cID=([^&]*)/);
// ["cID=876XYZ964D293CF", "876XYZ964D293CF"]

// match[0] is the whole pattern
// match[1] is the first capture group - ([^&]*)
// match will be 'false' if the match failed entirely
Agrology answered 9/6, 2010 at 8:10 Comment(0)
F
4

By using capturing groups:

cID=([^&]*)

and then get $1:

87B6XYZ964D293CF
Fairminded answered 9/6, 2010 at 5:43 Comment(0)
V
2

Here's the Javascript code:

 var str = "http://example.com/createSend/step4_1.aspx?cID=876XYZ964D293CF&snap=true&jlkj=kjhkjh&";
    var myReg = new RegExp("cID=([^&]*)", "i");
    var myMatch = myReg.exec(str);
    alert(myMatch[1]);
Varick answered 9/6, 2010 at 6:51 Comment(0)
P
1

There is a special syntax in javascript which allows you to exclude unwanted match from the result. The syntax is "?:" In your case the solution would be the following

'http://example.com/createSend/step4_1.aspx?cID=876XYZ964D293CF&snap=true&jlkj=kjhkjh&'.match(/(?:cID=+)([^&]*)/)[1];
Premature answered 25/12, 2014 at 1:54 Comment(1)
This is excessive. 'http://example.com/createSend/step4_1.aspx?cID=876XYZ964D293CF&snap=true&jlkj=kjhkjh&'.match(/cID=+([^&]*)/)[1]; is simpler as it does not require the non-capturing group and returns the same result as your example.Urson

© 2022 - 2024 — McMap. All rights reserved.