var regexLiteral = /cat/;
RegExp
object's constructor, which is passed a string from which it creates a regular expression: var regexConstructor = new RegExp("cat");
c
, followed by the character a
, followed by the character t
.RegExp
constructor..test()
, which returns a boolean value: RegExp.prototype.test()
true
if the string contains a match with the specified regular expression pattern. If no match is found, it returns false
. const str1 = "the cat says meow"; const str2 = "the dog says bark"; const hasCat = /cat/; hasCat.test(str1); // true hasCat.test(str2); // false
str1
, for the presence of a sequence of cat
characters in it, we get true
. But after checking the second line, str2
, we do not find cat
in it, so the .test()
method returns false
..
(dot) - matches any single character except for line breaks.*
- corresponds to the previous expression, which is repeated 0 or more times.+
- matches the previous expression, which is repeated 1 or more times.?
- corresponds to the previous expression, repeated 0 or 1 time.^
- matches the beginning of the line.$
- matches the end of the line.\d
matches any single numeric character.\w
- matches any character - a digit, letter, or underscore.[XYZ]
- a set of characters. Matches any single character in the set specified in brackets. In addition, character ranges can be specified in a similar way, for example, [AZ]
.[XYZ]+
- Matches a character from brackets, repeated one or more times.[^AZ]
- inside expressions defining character ranges, the symbol ^
used as a negation sign. In this example, the pattern matches everything that is not uppercase./[AZ]/g
. We will consider here only two flags:g
- global search by string.i
- case-insensitive search.(x)
- exciting brackets. This expression corresponds to x
and remembers this correspondence; as a result, we can use it later.(?:x)
- non-capturing brackets. The expression matches x
, but does not remember this match.x(?=y)
is a forward match. Matches x
only if it is followed by y
.\d
pattern. Take a look at the code below. It returns true
in cases where there is at least one digit in the string under study. console.log(/\d/.test('12-34')); // true
true
- this is not surprising, since there are four numeric characters in the string under study.\d
pattern repeated several times. For example, in order for a regular expression to match line 11
, you can use the \d\d
construct, which describes any two consecutive numeric characters. Take a look at this code: console.log(/\d-\d-\d-\d/.test('1-2-3-4')); // true console.log(/\d-\d-\d-\d/.test('1-23-4')); // false
+
sign to indicate that the /d
pattern may occur one or more times. Here's what it looks like: console.log(/\d+-\d+/.test('12-34')); // true console.log(/\d+-\d+/.test('1-234')); // true console.log(/\d+-\d+/.test('-34')); // false
console.log(/me+(ow)+w/.test('meeeeowowoww')); // true
/me+(ow)+w/
m
- corresponds to a single letter m
.e+
- matches the letter e
, repeated one or more times.(ow)+
matches the combination of ow
repeated one or more times.w
- corresponds to a single letter w
. 'm' + 'eeee' +'owowow' + 'w'
+
are used immediately after the expressions enclosed in brackets, they refer to everything that is in brackets.?
. The question mark indicates that the presence of the character preceding it in the string is optional. console.log(/cats? says?/i.test('the Cat says meow')); // true console.log(/cats? says?/i.test('the Cats say meow')); // true
true
. This is because we made the s
characters at the end of the sequences cat
and say
optional. In addition, you can notice that at the end of the regular expression is the flag i
. Thanks to him when analyzing strings ignored case of characters. That is why a regular expression reacts to both the cat
line and the Cat
line.+
?
, and others, have a special meaning. If you need to organize a search in the strings of these special characters, they need to be escaped with a backslash. Here's what it looks like: var slash = /\//; var qmark = /\?/;
\d
is the same as [0-9]
. Each of these expressions matches any numeric character.\w
is the same as [A-Za-z0-9_]
. Both will find in the string any single alphanumeric character or underscore.CamelCase
, and adds spaces between the individual words of which it is composed. Using a ready-made function, which we call removeCc
, looks like this: removeCc('camelCase') // => 'camel Case'
function removeCc(str){ // }
return
expression of this function some kind of construction that uses regular expressions that process the input data. In order to do this, you first need to find all capital letters in the string, using a construction that defines a range of characters and provides a global search in the string. /[AZ]/g
C
in the string camelCase
. How to add a space before this letter C
? // /([AZ])/ // $1
$1
to refer to the captured value. It is worth noting that if there are two sets of exciting brackets in the expression, you can use the expressions $1
and $2
to refer to the captured values ββin the order they follow from left to right. In this case, exciting brackets can be used as many times as needed in a particular situation.(?:x)
In this example, there is a match with x
, but it is not remembered.String
object method that can be used to work with exciting brackets β this is .replace()
. In order to use it, we will search for any capital letters in the string. The second argument of the method, representing the replacement value, will be the stored value: function removeCc(str){ return str.replace(/([AZ])/g, '$1'); }
$1
variable. As a result, before each capital letter in the string that the function returns, there will be a space. As a result, we got the following: function removeCc(str){ return str.replace(/([AZ])/g, ' $1'); } removeCc('camelCase') // 'camel Case' removeCc('helloWorldItIsMe') // 'hello World It Is Me'
/[AZ]/g
.replace()
method already familiar to you, but this time we will need something new when calling this method. This is how the outline of what we need will look like. Question marks indicate this new, as yet unknown, code: function lowerCase(str){ return str.replace(/[AZ]/g, ???); }
.replace()
method is remarkable in that we can use a function as its second parameter. This function will be called after a match is found, and what this function returns will be used as a string, replacing what the regular expression has found..toLowerCase()
method of the String
object to convert the input string to the desired form. Here is how, taking into account the above, the solution to our problem looks like: function lowerCase(str){ return str.replace(/[AZ]/g, u => u.toLowerCase()); } lowerCase('camel Case') // 'camel case' lowerCase('hello World It Is Me') // 'hello world it is me'
capitalize('camel case') // => 'Camel case'
.replace()
method. However, this time we need to find only the very first character of the string. In order to do this, use the symbol ^
. Recall one of the above examples: console.log(/cat/.test('the cat says meow')); // true
^
symbol to the beginning of the template, true
this construction will not return. This will happen because the word cat
is not at the beginning of the line: console.log(/^cat/.test('the cat says meow')); // false
^
affect any lower-case character at the beginning of a line. Therefore, we add it right before the [az]
construct. As a result, the regular expression will only respond to the first letter of the string in lowercase: /^[az]/
.toUpperCase()
: function capitalize(str){ return str.replace(/^[az]/, u => u.toUpperCase()); } capitalize('camel case') // 'Camel case' capitalize('hello world it is me') // 'Hello world it is me'
function removeCc(str){ return str.replace(/([AZ])/g, ' $1'); } function lowerCase(str){ return str.replace(/[AZ]/g, u => u.toLowerCase()); } function capitalize(str){ return str.replace(/^[az]/, u => u.toUpperCase()); } capitalize(lowerCase(removeCc('camelCaseIsFun'))); // "Camel case is fun"
Source: https://habr.com/ru/post/343798/
All Articles