Regex |
RegexRegular Expression - Tech and Technology |
RegexWhat is RegEx? RegEx means Regular Expression, it is used JavaScript and other Platforms and Programming Languages. Example in javascript: var regex = /[A-Za-z0-9]/; |
The / and / characters are used to start and end the regular expression. |
The //ig : ig means global replace |
[A-Z] Matches any uppercase character from A to Z . |
[0-9] Matches any whole numbers from 0 to 9 . |
/[^0-9]/ not a digit |
[a-zA-Z0-9] Matches any character from a to z or A to Z or 0 to 9 . |
[^abc] Matches any character except a , b or c . |
[] The square brackets are used to match any one of multiple specified patterns. |
\w which is equivalent to RegEx \[A-Za-z0–9_]\ |
\W To skip all numbers and letters |
\d To match only digits |
\D To not match digits |
$ character matches the end of the string. |
/^[A-Za-z0-9]*$/ |
/^[a-z0-9]+$/i |
\s is a whitespace, in a regular expression matches any one of the characters: space, formfeed ( \f ), newline ( \n ), return ( \r ), tab ( \t ), vertical tab ( \v ), non-breaking space ( \xA0 ), as well as the Unicode characters \u00A0 \u2028 \u2029 . |
The ^ character matches the beginning of the string, |
The RegExp test() method searches for a match between the regular expression and a specified string. |
The * character matches zero or more occurrences of a particular pattern. We add it after the square brackets to match any of the patterns in the brackets as many times as possible. |
The String replace() method returns a new string with some or all matches of a specified pattern replaced by a replacement. We use an empty string ('') as the replacement to have all the letters and numbers removed in the resulting string. |
We use the g (global) flag to match all the occurrences of the pattern in the string. If we don’t specify this flag, only the first match of a letter or number will be removed. |
We pass the result of match() to the Boolean constructor to convert it to a Boolean. Boolean() converts truthy values to true, and falsy values to false. |
In JavaScript, there are six falsy values: undefined, null, NaN, 0, '' (empty string), and false. Every other value is truthy. |
Example in javascript:
var myString = ' |