0

I need to use new RegExp I need to get a match if there is a specific string between two characters, but no match if that is a similar string between those characters, / and ?. I.e:

String to match is:

"https://www.mysite.se/should-match?ba=11"

And I have should-ma

That should not give any match. But should-match should give a match So I need to create new RegExp()

Any ideas?

user1665355
  • 3,324
  • 8
  • 44
  • 84
  • https://stackoverflow.com/questions/1454913/regular-expression-to-find-a-string-included-between-two-characters-while-exclud – adprocas Jun 05 '17 at 18:53
  • That's for PHP, but you should be able to use the regex within JavaScript as well. There are multiple sites that will let you test this as well. https://regex101.com/ – adprocas Jun 05 '17 at 18:54
  • @adpro Ok, thanks. but how to match specific text between `/` and `?` ? – user1665355 Jun 05 '17 at 18:58
  • \/(.*?)\? - that's fairly close - I'm sure you can take it from there. This would give you '/www.mysite.se/should-match', so you'll have to refine a bit better. – adprocas Jun 05 '17 at 19:00

1 Answers1

0

Try this:

(?!\/)[^\/\?]*(?=\?)

(test it out)

Replace \/ with the start delimiter and \? with the end delimiter. If the start or end delimiters have any of .?*+^$[]\(){}|- in them, you will need to add a \ before them, or use this function to do the work for you:

var escape = function(str) {
    return (str+'').replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&");
};

Alternative:

var matcher = function(str, start, end){
    var quote = function(str) {
        return (str+'').replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&");
    };
    return str.match(new RegExp(quote(start) + "[^" + quote(start) + quote(end) + "]*" + quote(end)))[0].slice(1, -1)
};

Use like matcher("https://www.mysite.se/should-match?ba=11", "/", "?").

  • Thanks but I only need what to put inside `new RegExp`. Please answer that and I accept :) – user1665355 Jun 05 '17 at 19:13
  • @user1665355 fixed. –  Jun 05 '17 at 19:16
  • The problem is still that I use it in mongo and I have a string already, say ``should-ma` and I only must have `new Regexp`. I.e. `MyField: new RegExp(...)` – user1665355 Jun 05 '17 at 19:20
  • @user1665355 does `(?!\/)[^\/\?]*(?=\?)` not work for you? –  Jun 05 '17 at 19:21
  • it works but I `need` to put my own word, not match what is between `/` and `?`. Do you understand? :) – user1665355 Jun 05 '17 at 19:22
  • @user1665355 What characters do you need to match between? –  Jun 05 '17 at 19:23
  • between `/` and `?`. So new RegExp(should-ma) should not match but new RegExp(should-match) should match. It is a mongodb field I work with. So I `must put a variable inside new RegExp`, between `/` and `?` – user1665355 Jun 05 '17 at 19:24
  • No, because it needs to match exactly. So even if `should-ma` is contained in `should-match`, it should not match. So no match if it contains.I.e. the string between `/` and `?` should be exact. – user1665355 Jun 05 '17 at 21:58
  • @user1665355 what about `\/should-match\?`? –  Jun 06 '17 at 00:52
  • No the pattern between / and ? MUST be exactly what I put in. So "should-ma" should not match www.mysite.com/should-match?. Now it does. Basically pattern can not be contained in pattern in my url – user1665355 Jun 06 '17 at 07:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145972/discussion-between-programmer5000-and-user1665355). –  Jun 06 '17 at 12:34