A quick basic overview of regular expressions in JavaScript
When using regular expressions you can define the expression on two ways.
- Either directly in the function or variable by using
/regular expression/
- Or by using the regExp contructor:
new RegExp('regular expression')
.
Please note the difference between the two ways of defining. In the first the search pattern is encapsuled by forward slashes
, while in the second one the search pattern is passed as a string
.
Remember that regular expressions is in fact a search language with it's own syntax. Some characters are used to define actions: /, \, ^, $, . (dot), |, ?, *, +, (, ), [, {, ', "
. These characters are called metacharacters and need to be escaped if you want them to be part of the search pattern. If not they will be treated as an option or generate script errors. Escaping is done by using the backslash. E.g. \\
escapes the second backslash and the search pattern will now search for backslashes.
There are a multitude of options you can add to your search pattern.:
Examples
adding \d
will make the pattern search for a numeric value between [0-9] and/or the underscore. Simple regular expressions are parsed from left to right.
/javascript/
Searches for the word javascript
in a string.
/[a-z]/
When a pattern is put between square bracket the search pattern searches for a character matching any one of the values inside the square brackets. This will find d
in 229302d34330
You can build a regular expression with multiple blocks.
/(java)|(emca)script/
Find javascript or emcascript in a string. The |
is the or
operator.
/a/ vs. /a+/
The first matches the first a
in aaabbb
, the second matches a repetition of a
until another character is found. So the second matches: aaa
.
The plus sign +
means find a one or more times. You can also use *
which means zero or more times.
/^\d+$/
We've seen the \d
earlier and also the plus sign. This means find one or more numeric characters. The ^
(caret) and $
(dollar sign) are new. The ^
says start searching from the begin of the string, while the $
says until the end of the string. This expression will match: 574545485
but not d43849343
, 549854fff
or 4348d8788
.
Flags
Flags are operators and are declared after the regular expression /regular expression/flags
JavaScript has three flags you can use:
g
(global) Searches multiples times for the pattern.
i
(ignore case) Ignores case in pattern.
m
(multiline) treat beginning and end characters (^
and $
) as working over multiple lines (i.e., match the beginning or end of each line (delimited by \n
or \r
), not only the very beginning or end of the whole input string)
So a regular expression like this:
/d[0-9]+/ig
matches D094938
and D344783
in 98498D094938A37834D344783
.
The i
makes the search case-insensitive. Matching a D
because of the d
in the pattern. If D
is followed by one or more numbers then the pattern is matched. The g
flag commands the expression to look for the pattern globally or simply said: multiple times.
In your case @Qwerty provided the correct regex:
origin_str.replace(/\//g, "")
Where the search pattern is a single forward slash /
. Escaped by the backslash to prevent script errors. The g
flags commands the replace function to search for all occurrences of the forward slash in the string and replace them with an empty string ""
.
For a comprehensive tutorial and reference : http://www.regular-expressions.info/tutorial.html