Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, January 12, 2016

How to match either `?` or `/` with regex?

How to match either `?` or `/` with regex?


I want to split a string at every position that contains a ? or a /. I am not very familiar with regular expressions, so this is giving me quite some trouble, since I am not sure where and what to escape. It should be something like this:

let uri = '/some/random/path?param=1234'  let arr = uri.split(/ ?|/ /);  

Answer by James Thorpe for How to match either `?` or `/` with regex?


Both ? and / need to be escaped in your regex using \:

let uri = '/some/random/path?param=1234'  let arr = uri.split(/\?|\//);  console.log(arr);  //arr = ["", "some", "random", "path", "param=1234"]

Answer by Tom Hart for How to match either `?` or `/` with regex?


How about something like this?

var uri = '/some/random/path?param=1234';  var split = uri.split(/([?\/])/g);  console.log(split);  

https://jsfiddle.net/tomharto/0rjw01ch/

Answer by Lix for How to match either `?` or `/` with regex?


In order to match the literal characters / and ?, you'll need to escape them; since they have special meanings for regular expressions. To escape a special character, you should place a \ in front of it. So ? would become \?.

In addition, you have some space characters in your regex as well - unless you are actually trying to match blank spaces, you should leave them out. For example, /abc / will match the string "abc " - note the space character after the abc.

What you are left with is something similar to this:

var uri = '/some/random/path?param=1234'  var arr = uri.split(/\?|\//);  // ["", "some", "random", "path", "param=1234"]  

Answer by Wasiq Muhammad for How to match either `?` or `/` with regex?


Here it works.Check it

    var regex = /([\?|\/])\w+/g;      var uri = "'/some/random/path?param=1234'";      var final = uri.match(regex);      console.log(final);  

Answer by stribizhev for How to match either `?` or `/` with regex?


While everyone tries to prove that escaping is necessary, let me show you a way to keep off the escaping hell: using a RegExp constructor notation, you avoid having to escape / and inside a character class, you will only have to escape ] and sometimes -.

var uri = '/some/random/path?param=1234'  var arr = uri.split(RegExp("[?/]"));  document.body.innerHTML = "
" + JSON.stringify(arr, 0, 4) + "
"; // with empty document.body.innerHTML += "
" + JSON.stringify(arr.filter(Boolean), 0, 4) + "
"; // no empty

Note that you do not need a /g modifier with String#split(), it is default behavior.

Some more notes on escaping in JavaScript regex patterns:

  • \ must always be escaped
  • ? must be escaped outside of a character class
  • / must be escaped in a literal regex notation
  • [ must be escaped when outside of a character class
  • ] must be escaped inside a character class
  • +, *, (, ), ^, $, . must be escaped outside the character class and do not have to be escaped inside a character class.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.