Skip to content

Operands

Matteo Basso edited this page Dec 27, 2016 · 18 revisions

Here you can find the list of the operands. Please note that for the complex ones, called Helpers, we have a separate list defined here

In natural regex a number always represents an operand, this means that you can do something like this:

const number = NaturalRegex.from('49');
number.test('49'); // true
number.test('50'); // false

In natural regex a single character always represents an operand, this means that you can do something like this:

const a = NaturalRegex.from('a');
a.test('a'); // true
a.test('b'); // false

In addition to this, there are some character that can be represented with a natural notation. For example, you can match a comma with comma or with ,. Here is an example:

const comma = NaturalRegex.from(',');
const comma = NaturalRegex.from('comma');

The following table has all the characters that can be represented in this way

Character Natural notation
, comma
. point
; semicolon
^ caret
+ plus
- minus
_ underscore
* asterisk, star
? question mark
( left round bracket
) right round bracket
{ left curly bracket
} right curly bracket
[ left square bracket
] right square bracket
: colon
! exclamation mark
$ dollar
` `
" quotation mark
\ backslash
/ slash

In natural regex a word always represents an operand, this means that you can do something like this:

const foo = NaturalRegex.from('foo');
foo.test('foo'); // true
foo.test('bar'); // false

A word can contain any character except "," and ".", if you want to use them, you can do something like this:

const fooAndBar = NaturalRegex.from('foo, comma, space and then bar');
fooAndBar.test('foo, bar'); // true

If you want to match literally a word like "comma" that is a language keyword, or a word like "operand", that ends with the language keyword "and", you can escape it with double quotes:

const comma = NaturalRegex.from('starts with "comma"');
comma.test('comma'); // true
comma.test(','); // false

const operand = NaturalRegex.from('operand'); // parsing error
const operand = NaturalRegex.from('"operand"'); // it works

Matches beginning of input. Without writing this, a regular expression can match a portion of text that is not necessary located at the beginning of the string.

const oo = NaturalRegex.from('oo');
const startsWithOo = NaturalRegex.from('start, oo');
oo.test('foo'); // true, matches oo in foo
startsWithOo.test('foo'); // false, oo not at the beginning of the string

Matches end of input. Without writing this, the string can contains other character after the match.

const oo = NaturalRegex.from('oo');
const endsWithOo = NaturalRegex.from('oo, end');
oo.test('foo bar'); // true, matches oo in foo
endsWithOo.test('foo bar'); // false, oo not at the end of the string

Matches an alphanumeric character, including space, tab, form feed, line feed.

const space = NaturalRegex.from('space');
space.test(' '); // true
space.test('_'); // false

Matches any non space character.

const nonSpace = NaturalRegex.from('non space');
nonSpace.test(' '); // false
nonSpace.test('_'); // true

Matches an alphanumeric character (lowercase and uppercaseletter from A to Z, numbers from 0 to 9 and underscore).

const alphanumeric = NaturalRegex.from('alphanumeric');
alphanumeric.test('9'); // true
alphanumeric.test('_'); // true
alphanumeric.test('a'); // true
alphanumeric.test('&'); // false

Matches any non alphanumeric character (anything except lowercase and uppercaseletter from A to Z, numbers from 0 to 9 and underscore).

const nonAlphanumeric = NaturalRegex.from('non alphanumeric');
nonAlphanumeric.test('9'); // false
nonAlphanumeric.test('_'); // false
nonAlphanumeric.test('a'); // false
nonAlphanumeric.test('&'); // true

Matches a single digit character (number from 0 to 9).

const digit = NaturalRegex.from('digit');
digit.test('7'); // true
digit.test('a'); // false

Matches any non digit character (anything except numbers from 0 to 9).

const nonDigit = NaturalRegex.from('non digit');
nonDigit.test('7'); // false
nonDigit.test('a'); // true

Matches any character.

const anycharacter = NaturalRegex.from('any character');
anycharacter.test('7'); // true
anycharacter.test('a'); // true
anycharacter.test('%'); // true

Matches any character or number in the given range (case sensitive).

// lowercase
const aToD = NaturalRegex.from('from a to d');
aToD.test('a'); // true
aToD.test('e'); // false, out of range
aToD.test('A'); // false, uppercase
// uppercase
const aToD = NaturalRegex.from('from A to Z');
// numbers
const zeroToFour = NaturalRegex.from('from 0 to 4');
aToD.test('0'); // true
aToD.test('5'); // false, out of range

Matches a tab (U+0009).

Matches a vertical tab (U+000B).

Matches a null character (U+0000).

Matches a carriage return (U+000D).

Matches a line feed (U+000D).

Matches a form feed (U+000C).

Matches a backspace (U+0008).

Use hex with 2 or 4 hexadecimal digits to match the character with that code.

// 61 is the hexademical representation of character 'a'
const a = NaturalRegex.from('hex 61');
a.test('a'); // true
a.test('b'); // false

Use unicode with 4 or 5 hexadecimal digits to match the character with that code. This can be used only if sticky flag (y) is active.

// 0061 is the hexademical representation of character 'a'
const a = NaturalRegex.from('unicode 0061');
a.test('a'); // true
a.test('b'); // false

Use ctrl+ with a letter from A to Z (uppercase) to match CTRL+letter.

// this is the representation of CTRL+C
const ctrlC = NaturalRegex.from('ctrl+C');