escript:regular_expressions
Розбіжності
Тут показані розбіжності між вибраною ревізією та поточною версією сторінки.
| Наступна ревізія | Попередня ревізія | ||
| escript:regular_expressions [2014/10/27 22:50] – создано wiki_admin | escript:regular_expressions [2022/05/23 15:26] (поточний) – зовнішнє редагування 127.0.0.1 | ||
|---|---|---|---|
| Рядок 1: | Рядок 1: | ||
| + | ====== Регулярные выражения в Siebel eScript пример использования. ====== | ||
| + | |||
| + | Наша задача написать функцию которая будет возвращать ошибку в случаи если в поле ИНН клиента содержится любой символ кроме числа. | ||
| + | |||
| + | Задача на первый взгляд достаточно простая. Делаем тип поля число и вопрос решен, но не все так просто. | ||
| + | |||
| + | ИНН клиента юр. лица может начинаться с числа ’0′ и как мы помним из уроков математики ’0′ в начале числа убирается. :( Учитывая это нам необходимо делать тип поля текст что в свою очередь разрешает запись любого символа. | ||
| + | |||
| + | |||
| + | Пример ниже проверяет каждый символ входящей переменной используя регулярные выражения. В случаи если символ не число формирует ошибку и выводит её в дальнейшем. | ||
| + | |||
| + | ==== Вариант №1 ==== | ||
| + | <file javascript example.js> | ||
| + | function CheckINN(& | ||
| + | { | ||
| + | var rExp=""; | ||
| + | var v_i1 = 0; | ||
| + | var msgText = ""; | ||
| + | // | ||
| + | rExp = new RegExp(/ | ||
| + | |||
| + | // | ||
| + | | ||
| + | |||
| + | // | ||
| + | for (var i =0; i < text.length; | ||
| + | { | ||
| + | // | ||
| + | if ((!rExp.test(text.charAt(i)))) | ||
| + | { | ||
| + | | ||
| + | | ||
| + | | ||
| + | } | ||
| + | } | ||
| + | |||
| + | return msgText; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | ==== Вариант №2 ==== | ||
| + | Как вариант можно использовать следующее: | ||
| + | <code javascript> | ||
| + | if (text.match(/ | ||
| + | </ | ||
| + | |||
| + | ==== JavaScript Regular Expression Syntax ==== | ||
| + | |||
| + | Element | ||
| + | [xyz] Any one character x or y or z, as listed in square brackets\\ | ||
| + | [x-z] Any one character within the range specified in brackets\\ | ||
| + | [^xyz] | ||
| + | [^x-z] | ||
| + | . Any character except newline\\ | ||
| + | \d \D Any digit (\d), any non-digit (\D) \\ | ||
| + | \s \S Any whitespace (\s), any non-whitespace (\S) \\ | ||
| + | \w \W Any word character (\w), any non-word character (\W) \\ | ||
| + | \b \B Word boundary (\b), non-boundary (\B)\\ | ||
| + | [\b] | ||
| + | | ||
| + | | ||
| + | \n \r \f \t \v | ||
| + | \+ \- \. \* \? \| Match + - . * ? | etc. verbatim (not as a special character)\\ | ||
| + | \/ \\ \^ \$ Match / \ ^ $ verbatim (not as a special character)\\ | ||
| + | \[ \] \( \) \{ \} Match brackets/ | ||
| + | \xAB Match the character with hexadecimal code AB\\ | ||
| + | \uABCD | ||
| + | |||
| + | x|y Match x or y\\ | ||
| + | + Match the preceding element one or more times\\ | ||
| + | * Match the preceding element zero or more times\\ | ||
| + | ? Match the preceding element zero or one time\\ | ||
| + | {n} Match the preceding element exactly n times\\ | ||
| + | {n,} Match the preceding element n or more times\\ | ||
| + | {m,n} Match the preceding element at least m, at most n times\\ | ||
| + | |||
| + | (...) Match ... as a capturing group: store the matching substring\\ | ||
| + | (used with string.match(re) to return an array of matching strings)\\ | ||
| + | \1 \2 \3 etc. Match the same substring as in capturing group number 1, 2, 3 etc. \\ | ||
| + | $1 $2 $3 etc. | ||
| + | (used in the second argument of replace() method)\\ | ||
| + | (?: | ||
| + | |||
| + | Any character | ||
| + | (except the above)\\ | ||
| + | RegExp flags can be any combination of the following: | ||
| + | |||
| + | i | ||
| + | g | ||
| + | m | ||
| + | The effect of not setting these flags is as follows:\\ | ||
| + | i not set The regular expression is case-sensitive\\ | ||
| + | g not set Use only the first match (no global search)\\ | ||
| + | m not set ^ and $ match the beginning/ | ||
| + | Notes\\ | ||
| + | (1) A whitespace (\s) 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. | ||
| + | |||
| + | (2) When using the RegExp constructor: | ||
| + | |||
| + | re = / | ||
| + | re = new RegExp(' | ||
| + | Copyright © 1999-2011, JavaScripter.net. | ||
