Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Regular Expression
- RegEx is a text string used to describe a search pattern (of data like text etc.).
- Syntax: /regex/
- Literal character /thewordyourelookingfor/
- Flags or modes go after your last /
- /regex/ stops when it finds a match
- /regex/g looks for match til end of data (doesn't give up)
- /regex/i makes regexcase insensitive (otherwise it regex is cs)
- /regex/m multiline
- /regex/ single line. Allows dot to match newline \n
- Metacharacters: \|(){[$.^*+?
- These can't be used in literal character search.
- If you want to look for any of these 12 char., you have to
- escape them by adding a \ in front of them
- /(Hey)/ will result in an error. Instead you should write /\(hey\)/
- The dot is a wild character. It will match any character except for a newline. To make the dot also match newline you can use the flag/mode single line(s) /regex/gs
- Example
- Test string: Kawiesh kawiesh Kawiesh kaw9esh kaw99sh kAw?e$h
- /Kawiesh/ Kawiesh ------- ------- ------- ------- -------
- /Kawiesh/g Kawiesh ------- Kawiesh ------- ------- -------
- /Kawiesh/gi Kawiesh kawiesh Kawiesh ------- ------- -------
- /Kaw.esh/gi Kawiesh kawiesh Kawiesh kaw9esh ------- -------
- Dot is a place holder for only one character.
- Test string:
- Kaw
- Iesh
- /kaw.iesh/gi no match
- /kaw.iesh/gis match!
- ....... Other metacharacters.......
- \d matches all digits (0-9)
- \w matches all letters (lowercase or uppercase), all digits and underscore
- \s matches all white spaces (e.g.space, tab, new line)
- To match the opposite/inverse match, use capital letters
- E.g \D will match every character except digits.
Advertisement
Add Comment
Please, Sign In to add comment