انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة
الكلية كلية تكنولوجيا المعلومات
القسم قسم شبكات المعلومات
المرحلة 3
أستاذ المادة حۡــسۜــنۨ ا̍ڷــڔهــٻۧــمۘــې
15/04/2012 07:04:57
RegExp Object A regular expression is an object that describes a pattern of characters.Regular expressions are used to perform pattern-matching and "search-and-replace" functions on text.
Syntax
var patt=new RegExp(pattern,modifiers); or more simply: var patt=/pattern/modifiers;
Remarks : • pattern specifies the pattern of an expression for example "abcd" String pattern to find among string value
• modifiers specify if a search should be global, case-sensitive, or on multilines , modifiers can be mixed up together such as /patt/gim or /patt/gi ... etc , the mixing depends on the developer needs , Modifiers are used to perform case-insensitive and global searches , the main values are :
o i : Perform case-insensitive matching o g : Perform a global match (find all matches rather than stopping after the first match) o m : Perform multiline matching Brackets Brackets are used to find a range of characters that are specified inside these brakets "[]" , for example to find all numbers inside given string , you can describe the pattern as [the lowest number - the heighest number] as shown in the following examples :
Example 1 : var myString= my age is 27 because my bd is 1985 / 1/ 4 patt1=/[0-8]/g; document.write(str.match(patt1)); the output will be : 1,2,4,7,8 // but not 9 because its out the range
Example 2 : var myString= my age is 27 because my bd is 1985 / 1/ 4 patt1=/[a-d]/gi; document.write(str.match(patt1)); The output will be : a,b,c,a,b,d // only the characters in the string that are in the range a to d
Example 3: var myString= my age is 27 because my bd is 1985 / 1/ 4 patt1=/[m,y,s,4]/gi; document.write(str.match(patt1)); The output will be m,y,s,m,y,4
The Full Forms of a Pattern Modifier can be one of the following only :
• [abc] Find any character between the brackets • [^abc] Find any character not between the brackets • [0-9] Find any digit from 0 to 9 • [A-Z] Find any character from uppercase A to uppercase Z • [a-z] Find any character from lowercase a to lowercase z • [A-z] Find any character from uppercase A to lowercase z • [adgk]Find any character in the given set • [^adgk] Find any character outside the given set
Metacharacters Metacharacters are characters with a special meaning:
• . Find a single character, except newline or line terminator • \w Find a word character • \W Find a non-word character • \d Find a digit • \D Find a non-digit character • \s Find a whitespace character • \S Find a non-whitespace character • \b Find a match at the beginning/end of a word • \B Find a match not at the beginning/end of a word • \n Find a new line character • \r Find a carriage return character • \t Find a tab character • \v Find a vertical tab character
Notes : 1. The \s metacharacter is used to find a whitespace character. A whitespace character can be:
• A space character • A tab character • A carriage return character • A new line character • A vertical tab character
2. The \n character is used to find a newline character. The \n returns the position where the newline character was found. If no match is found, it returns -1.
Examples :
#1 : Do a global search for digits: var str="Give 100%!"; var patt1=/\d/g; The marked text shows where the expression gets a match: Give 100%!
#2 : Do a global search for whitespace characters in a string: var str="Is this all there is?"; var patt1=/\s/g; All the spaces in the text will get a match (four matches): Is this all there is?
#3 : Do a global search for "www" at the beginning or end of a word in a string: var str="Visit www.uobabylon.edu.iq"; var patt1=/\bwww/g; The marked text shows where the expression gets a match: www.uobabylon.edu.iq
#4 : Search for a newline character in a string: var str="check Links \n www.uobabylon.edu.iq \n repository.uobabylon.com"; var patt1=/\n/; The marked text shows where the expression gets a match:11
The \n Modifier only finds the first one when used with search() , and finds non when used with match() , but it works very well with Array String retruned string object methods like substring() or replace() , Example :
var str="check Links\n www.uobabylon.edu.iq\n repository.uobabylon.com"; var patt1=/\n/g; document.write(str.replace(patt1, ));
#5: Search for a single character between 2 characters or strings : var str="That s hxt!"; var patt1=/h.t/gi; document.write(str.match(patt1)); The marked text shows where the expression gets a match
Note : This Modifier finds only hat when the string is " That s hvbfgxt "
This lecture is to be continued ...
المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .
|