javascript’s regexp. regexp object javascript has an object which compiles regular expressions...

15
Javascript’s RegExp

Upload: giles-mcgee

Post on 05-Jan-2016

225 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Javascript’s RegExp. RegExp object Javascript has an Object which compiles Regular Expressions into a Finite State Machine The F.S.M. is internal, and

Javascript’s RegExp

Page 2: Javascript’s RegExp. RegExp object Javascript has an Object which compiles Regular Expressions into a Finite State Machine The F.S.M. is internal, and

RegExp object

Javascript has an Object which compiles Regular Expressions into a Finite State Machine

The F.S.M. is internal, and is stored in the RegExp object

var g = new RegExp;

Page 3: Javascript’s RegExp. RegExp object Javascript has an Object which compiles Regular Expressions into a Finite State Machine The F.S.M. is internal, and

RegExp object

uses extended grep as the language (which are typically referred to as Regular Expressions)

Compile the expression order to use it

Page 4: Javascript’s RegExp. RegExp object Javascript has an Object which compiles Regular Expressions into a Finite State Machine The F.S.M. is internal, and

RegExp object

new RegExp(”grep_string”, “options”);

Using it is FAST, which is why grep patterns are so popular

Creating/compiling the RegExp object is SLOW!

Page 5: Javascript’s RegExp. RegExp object Javascript has an Object which compiles Regular Expressions into a Finite State Machine The F.S.M. is internal, and

RegExp Options

Optional parameter (flags):

...new RegExp(”string”, “ig”)

regExpObj.compile(”string”, “gi”);

i= ignore case

g= global (find every match)

allows replace all + useful in loops

Page 6: Javascript’s RegExp. RegExp object Javascript has an Object which compiles Regular Expressions into a Finite State Machine The F.S.M. is internal, and

RegExp object

re= new RegExp( /querystring/ );

re.test( “someText” );

re.exec( “someText” );

returns an array of results

[0] is found string

[>=1] is found parts() in that string

Page 7: Javascript’s RegExp. RegExp object Javascript has an Object which compiles Regular Expressions into a Finite State Machine The F.S.M. is internal, and

Alternate Methods

Some browsers promote 1 method over another

var x = new RegExp(”string”);

vs (preferred)

var x = new RegExp(/string/);

vs (not recommended but shortest)

var x= /string/;

Page 8: Javascript’s RegExp. RegExp object Javascript has an Object which compiles Regular Expressions into a Finite State Machine The F.S.M. is internal, and

RegExp Options

multiline problem

m=multiline flag is NOT supported

multiline is a global boolean property of RegExp object itself

if not supported; you can test for it.

if( typeof(RegExp.multiline) == ‘Boolean’)

Page 9: Javascript’s RegExp. RegExp object Javascript has an Object which compiles Regular Expressions into a Finite State Machine The F.S.M. is internal, and

/Common Use/

/string/ similar to “quotes” on strings

if you use “string” you must escape:

/\d\d/ (match 2 digit pattern)

vs

“\\d\\d” (match 2 digit string)

Page 10: Javascript’s RegExp. RegExp object Javascript has an Object which compiles Regular Expressions into a Finite State Machine The F.S.M. is internal, and

Example

var re = new RegExp(”GREP”, “i”);

var str = “stringrep”;

if( re.test(str) )

alert(“found it!”);

Page 11: Javascript’s RegExp. RegExp object Javascript has an Object which compiles Regular Expressions into a Finite State Machine The F.S.M. is internal, and

Example 2

var re = /grep/i ;

var str = “stringrep”;

if( re.test(str) )

alert(“found it!”);

Page 12: Javascript’s RegExp. RegExp object Javascript has an Object which compiles Regular Expressions into a Finite State Machine The F.S.M. is internal, and

String Object

.search(regexp)

returns position found or -1

.replace(regexp, string)

returns new string or old string

.match(regexp)

returns array of matches or null

Page 13: Javascript’s RegExp. RegExp object Javascript has an Object which compiles Regular Expressions into a Finite State Machine The F.S.M. is internal, and

Example

var g = new RegExp(”grep”, “i”);

var str = “stringrep”;

if( str.search(g) )

alert(“found it!”);

Page 14: Javascript’s RegExp. RegExp object Javascript has an Object which compiles Regular Expressions into a Finite State Machine The F.S.M. is internal, and

Example 2

Preferred modern syntax:

var str = “stringrep”;

if( str.search( /grep/ ) )

alert(“found it!”);

Page 15: Javascript’s RegExp. RegExp object Javascript has an Object which compiles Regular Expressions into a Finite State Machine The F.S.M. is internal, and

Learning

Use a Text Editor that supports RegExp or Grep Patterns (even better if it claims Perl Reg. Exp.)

Use the Regular Expression Play Pen webpage (class website)

Use javascript (code, make your own test page, or javascript console)