-
Notifications
You must be signed in to change notification settings - Fork 10
Operators
Here you can find the list of the operators.
A separator is a special character that can be used to chain 2 expressions or to maintain a natural notation. For example, reminding that a string is an expression, we can match "foobar" or "foolorem" in 2 ways:
NaturalRegex.from('foobar or foolorem');
// or
NaturalRegex.from('foo and then bar or lorem');
Here is the list of the available separators
Separator | Usage |
---|---|
. |
e. or e. e
|
, |
e , e |
and |
e and e |
then |
e then e |
, then |
e , then e |
and then |
e and then e |
e one or more times
matches e
from one to infinite times:
const foo = NaturalRegex.from('foo one or more times');
foo.test('foo'); // true
foo.test('foofoofoo'); // true
foo.test('bar'); // false
e optional more times
matches e
from zero to infinite times:
const foo = NaturalRegex.from('foo, o optional more times');
foo.test('foo'); // true, third o is optional
foo.test('foooooooo'); // true
foo.test('fo'); // false
e optional
matches e
from zero to one time:
const foo = NaturalRegex.from('foo, bar optional');
foo.test('foo'); // true, bar is optional
foo.test('foobar'); // true
foo.test('foo lorem'); // false
e minimum {number} times
matches e
from {number}
to infinite times:
const foo = NaturalRegex.from('foo minimum 3 times');
foo.test('foo'); // false, 1 time
foo.test('foofoo'); // true
foo.test('foofoofoo'); // true
foo.test('foofoofoofoo'); // true
e maximum {number} times
matches e
from one to {number}
times:
const foo = NaturalRegex.from('foo maximum 3 times');
foo.test('foo'); // true
foo.test('foofoo'); // true
foo.test('foofoofoo'); // true
foo.test('foofoofoofoo'); // false, 4 times
e from {number} to {number} times
matches e
from {number}
to {number}
times:
const foo = NaturalRegex.from('foo from 2 to 4 times');
foo.test('foo'); // false, 1 time
foo.test('foofoo'); // true
foo.test('foofoofoo'); // true
foo.test('foofoofoofoofoo'); // false, 5 times
e for {number} times
matches e
exactly {number}
times:
const foo = NaturalRegex.from('foo for 3 times');
foo.test('foo'); // false, 1 time
foo.test('foofoofoo'); // true
foo.test('foofoofoofoofoo'); // false, 5 times
(smallest)
after a repetition operator, matches the smallest possible match, here is an example:
NaturalRegex.replace({
string: '<foo><bar /></foo>',
match: `
starts with <,
any character optional more times,
>
`,
replace: 'bar',
});
// without (smallest) this matches the entire string and returns: 'bar'
// this is because it searches the last >
NaturalRegex.replace({
string: '<foo><bar /></foo>',
match: `
starts with <,
any character optional more times (smallest),
>
`,
replace: 'bar',
});
// with (smallest) this matches only the first tag and returns: 'bar<bar /></foo>'
// this is because it searches the first >
e or e
matches only one between the first and the second e
. For example, if we want to match only one between foo
and bar
we can write:
const fooOrBar = NaturalRegex.from('foo or bar');
fooOrBar.test('foo'); // true
fooOrBar.test('bar'); // true
fooOrBar.test('foobar'); // false
fooOrBar.test('foofoo'); // false
starts with e
indicates that a string must start with the given expression. Reminding to start operand, these 2 are equal:
NaturalRegex.from('start, foo');
NaturalRegex.from('starts with foo');
ends with e
indicates that a string must end with the given expression. Reminding to end operand, these 2 are equal:
NaturalRegex.from('foo, end');
NaturalRegex.from('ends with foo');
in charset: c0, c1, c2, ...., cn;
matches a single character cn
only if it is present in the given list for example:
const inCharset = NaturalRegex.from('in charset: a, c, e;');
inCharset.test('a'); // true
inCharset.test('c'); // true
inCharset.test('x'); // false
You can also use a range of character or number:
const inCharset = NaturalRegex.from('in charset: from a to z, from 0 to 9;');
inCharset.test('a'); // true
inCharset.test('7'); // true
inCharset.test('A'); // false, uppercase
in charset: c0, c1, c2, ...., cn;
matches a single character cn
only if it is not present in the given list for example:
const notInCharset = NaturalRegex.from('not in charset: a, c, e;');
notInCharset.test('a'); // false
notInCharset.test('c'); // false
notInCharset.test('x'); // true
You can also use a range of character or number:
const notInCharset = NaturalRegex.from('not in charset: from a to c, from h to z;');
notInCharset.test('a'); // false
notInCharset.test('e'); // true
notInCharset.test('z'); // false
group e end group
is a special operator that wrap explicitly the given e
inside a group. In this way you can apply and operator to the entire group, for example:
// without group
const noGroup = NaturalRegex.from('foo, space and then bar one or more times');
noGroup.test('foo barbar'); // true
noGroup.test('foo barfoo bar'); // false
// with group
const withGroup = NaturalRegex.from(`
group
foo, space and then bar
end group
one or more times
`);
withGroup.test('foo barbar'); // true
withGroup.test('foo barfoo bar'); // false
capture e end capture
is the same as group e end group
but remember the group, see this for more information
const capture = NaturalRegex.from(`
capture
foo, space and then bar
end capture
one or more times
`);
capture.test('foo barbar'); // true
capture.test('foo barfoo bar'); // false
e followed by e
is the same as not followed by
but without negation, here is the correct example with it:
NaturalRegex.replace({
string: 'foo,bar, lorem ipsum!',
match: 'foo, followed by non space',
replace: ', ',
});
// foo, bar, lorem ipsum!
e not followed by e
matches the first e
only if it is not followed by the second. This is different from a separator, that matches both the first and the second e
.
For example, consider the case when we want to have a string in which all commas are followed by a space.
If we have a string like this foo,bar, lorem ipsum!
, we must perform some replacement.
We can try with something like this:
NaturalRegex.replace({
string: 'foo,bar, lorem ipsum!',
match: ',',
replace: ', ',
});
// foo, bar, lorem ipsum!
In this case we obtain two spaces after bar
, that is not so good, so we can try with a different match: replace only if there is a comma and a non-space
NaturalRegex.replace({
string: 'foo,bar, lorem ipsum!',
match: 'foo, non space',
replace: ', ',
});
// foo, ar, lorem ipsum!
As we can see there is only one space after bar
, but bar
becomes only ar
, this is because we have replaced both the comma and the letter.
Now we can try with not followed by
, using this, we can match only the comma followed by a non space.
NaturalRegex.replace({
string: 'foo,bar, lorem ipsum!',
match: 'foo, not followed by space',
replace: ', ',
});
// foo, bar, lorem ipsum!