Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the regular expression \|(?=\w=>) mean?

I am an amateur in JavaScript. I saw this other (now deleted) question, and it made me wonder. Can you tell me what does the below regular expression exactly mean?

split(/\|(?=\w=>)/)

Does it split the string with |?

like image 716
NoOne Avatar asked May 03 '10 10:05

NoOne


People also ask

What does W mean in regular expression?

\w (word character) matches any single letter, number or underscore (same as [a-zA-Z0-9_] ). The uppercase counterpart \W (non-word-character) matches any single character that doesn't match by \w (same as [^a-zA-Z0-9_] ). In regex, the uppercase metacharacter is always the inverse of the lowercase counterpart.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.

What does \d mean in regex?

The RegExp \D Metacharacter in JavaScript is used to search non digit characters i.e all the characters except digits. It is same as [^0-9]. Syntax: /\D/ or new RegExp("\\D")


2 Answers

The regular expression is contained in the slashes. It means

\|        # A pipe symbol. It needs to be scaped with a backslash
          # because otherwise it means "OR"
(?=       # a so-called lookahead group. It checks if its contents match 
          # at the current position without actually advancing in the string
   \w=>   # a word character (a-z, A-Z, 0-9, _) followed by =>
)         # end of lookahead group.
like image 77
Jens Avatar answered Nov 03 '22 12:11

Jens


It splits the string on | but only if its followed by a char in [a-zA-Z0-9_] and =>

Example:

It will split a|b=> on the |

It will not split a|b on the |

like image 36
codaddict Avatar answered Nov 03 '22 10:11

codaddict