Before the advent of hypertext languages, but rather, until it became clear that you need not only to search, but also to do it under certain conditions, in a specific place, with changed data, in the right quantities, the usual search and replace functions would suit any sophisticated the programmer. Masterpieces of search art in programming languages were created, and databases were refined in the form of sampling conditions, equipped with stored procedures, triggers, and other means of selecting from bulky relational information compositions. The appearance of regular expressions did not lead to a revolution, but it turned out to be a useful and convenient tool for searching and replacing information. For example, regular JavaScript email expressions greatly simplify the registration of visitors, do not load the site by sending messages to non-existent addresses.
, JavaScript indexOf() , , , , , .
RegExp = +
- + . – JavaScript - RegExp, - , . , , . , , .
, , . . , «JavaScript ».
:
var expOne = /abc*/i;
var expTwo = RegExp(“abc*”, “i”);
. , '\', .
'i' - , « ». 'g' - « » 'm' - .
'/' .
'^' (), , '$' () . , .
,
var eRegExp = new RegExp(cRegExp, 'i');
var cRegRes = '';
var sTest = 'AbcZ';
if (eRegExp.test(sTest)) {
cRegRes += ' - Yes';
} else {
cRegRes += ' - No';
}
var dTestLine = document.getElementById('scTestLine');
dTestLine.innerHTML = ' /'+cRegExp+'/ "'+ sTest+'"'+cRegRes.
'scTestLine' ( cRegExp ):
/^AbcZ$/ "abcz" - Yes
'i', :
/^AbcZ$/ "abcz" - No
, . /qwerty/ :
/qwerty/ "qwerty" - Yes
/qwerty/ "123qwerty456" - Yes
'^' :
/^qwerty/ "123qwerty456" – No
/^qwerty/ "qwerty456" - Yes
. : , [a-z], [A-Z], [0-9] - . , ( , ) . , , .
, : '*' = 0 ; '+' = 1 ; {1,} , '+'; {n} = n ; {n,} = n ; {n,m} = n m .
, . . [abcd] = [a-d] = : 'a', 'b', 'c' 'd'. . , : [^abcd] = 'a', 'b', 'c' 'd'. '?' , . '.' , . '\n', '\r', '\u2028' '\u2029'. '\s*|\S*' = '[\s|\S]*' , .
'[\s|\S]*' - , , . '\s' , '\S' - .
'\d' , '\D' . '\f', 'r' '\n' form-feed, carriage return line-feed.
– '\t', – '\v'. '\w' (, , ) = [A-Za-z0-9_].
'\W' [^A-Za-z0-9_]. , , '_'.
'\0' = NUL. '\xHH' '\uHHHH' = HH HHHH . H - .
.
, , , «» .
( ) , . , . , , JavaScript-, .
, , , .
JavaScript . .
, , – . . , .
var cRegExp = "[a-z]*.(png|jpg|gif)";
var eRegExp = new RegExp(cRegExp, 'i');
var cRegRes = '';
var sTest = 'picture.jpg';
if (eRegExp.test(sTest)) {
cRegRes += ' - Yes';
} else {
cRegRes += ' - No';
}
:
/[a-z]*.(png|jpg|gif)/ "picture.jpg" - Yes
/^[a-d][a-z]*.(png|jpg|gif)/ "picture.jpg" – No
/^[a-d][a-z]*.(png|jpg|gif)/ "apicture.jpg" - Yes
/^[a-d][a-z]*.(png|jpg|gif)/ "apicture.jg" - No
, , , . , «» .
RegExp - email
JavaScript , test exec, (String) (): search, split, replace match.
test , . : true/false.
JavaScript. email «, »:
var eRegExp = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
var sTest ='SlavaChip@sci.by' true, email-. eRegExp.test(sTest).
: e-Mail
exec , :
var aResult = eRegExp.exec(sTest);
cRegRes = '<br/>' + aResult.length + '<br/>';
for (var i=0; i<aResult.length; i++) {
cRegRes += aResult[i] + '<br/>';
}
:
9
Slava.Chip@sci.by
Slava.Chip
Slava.Chip
.Chip
undefined
sci.by
undefined
sci.by
sci.
. . , .
«»
JavaScript eMail , . , /^[\w-\.]+@[\w-]+\.[a-z]{2,3}$/i. .
, , , JavaScript . . , JavaScript ( ) : '\', '/' . , .
« ». JavaScript , (), : 123-45-67, (29) 1234567, 80291234567 +375291234567. . , . /^\d[\d\(\)\ -]{4,14}\d$/i .
JavaScript , , . , , . , .
/^\d+$/i , /^\d+\.\d+$/i .
JavaScript , , , , . .
-
JavaScript . , . - , . : '--' '..' . , HTML-, , . , 3, 2 , 32, , , .
. , , . « » , , .
,
JavaScript replace ( ) String . , .
var cRegExp = /([-]+)\s([-]+)\s([-]+)/i; // ''
var sTest = ' !';
var cRegRes = sTest.replace(cRegExp, "$2, $3, $1");
var dTestLine = document.getElementById('scTestLine');
dTestLine.innerHTML = ' '+cRegExp+' "'+ sTest+'" : '+cRegRes;
:
/([-]+)\s([-]+)\s([-]+)/i " !" : , , !
'' $n, n - ($1, $2, ...). , 1, 0.
, . , . - .
. .
, '*', '+' , , . , . JavaScript . .
, . , JavaScript, , .
JavaScript, String RegExp
, JavaScript, . JavaScript- , , .
, , . , , . , . , .
Regular expressions enhance the capabilities of strings, but require due respect. Debugging RegExp during its operation, even if it is possible to simulate it, is not a very interesting undertaking.
Understanding the structure and logic of the RegExp object, the meaning of the String object, the syntax and semantics of JavaScript is a sure guarantee of safe and reliable code, stable operation of each page and the site as a whole.