Title: String Manipulation
1Chapter 8
2Background
- We already looked at the String object (Chapter
4) - length
- charAt()
- indexOf()
- substr() and substring()
- toUpperCase() and toLowerCase()
- Now well look at some very powerful other
methods - split()
- match()
- replace()
- search()
3The split() Method
- Splits a string into an array of substrings based
on the location and existence of separators - Separation parameter
- var myString A,B,C
- var myTextArray myString.split(,)
- ch8_examp1.htm Example
4The replace() Method
- Used to search for a substring within a string
and where it finds it, it replaces it with the
replacement substring specified. - var myString The event will occur in May, the
21st of May - myCleanedupString myString.replace(May,June
) - The event will occur in June, the 21st of June
5The search() Method
- Allows you to search a string for a particular
piece of text. - If found, the character position is returned. If
not found, a 1 is returned
6The match() Method
- Returns an array of all matches resulting from
searching for a string within some text - Most useful when used with regular expressions
7Regular Expressions
- Provide a means of defining a pattern of
characters, which are used to split, search, or
replace characters in a string where they fit the
defined pattern - RegExp object
- A simple example
8Regular Expressions Special Characters
9Regular Expressions Special Characters
10Regular Expressions Special Characters
11Regular Expressions Special Characters
- ch8_examp2.htm Example
- Checks to see if a string you enter contains only
letters and numbers - RegExp is an Object and has Methods
- .test
12Regular Expressions Repetition Characters
13Regular Expressions Repetition Characters
14Regular Expressions Repetition Characters
- Examples
- \d-\d\d\d-\d\d\d\d
- Can be rewritten as
- \d-\d3-\d4
- Paula? Matches Paul or Paula
15Regular Expressions Position Characters
16Regular Expressions Position Characters
17Regular Expressions Position Characters
- Examples
- myPattern
- Matches an occurrence of myPattern if it is at
the beginning of a line. - myPattern
- Matches an occurrence of myPattern if it is at
the end of a line. - Boundaries
- boundaries.htm
18Integrating
- Complex string replacement
- Paul, Paula, Paulina, paul, Paul
- Want this to convert to
- Ringo, Paula, Paulina, Ringo, Ringo
- var myRegExp /Paul/gi
- Ringo, Ringoa, Ringoina, Ringo, Ringo
- var myRegExp /Paul\W/gi
- \W matches any non-word character so this regular
expression matches occurrences of Paul followed
by a non-word character. - Ringo Paula, Paulina, Ringo Paul
- Note the missing commas and final occurrence of
Paul
19Integrating
- One more try using boundaries
- var myRegExp /Paul\b/gi
- Ringo, Paula, Paulina, Ringo, Ringo
- Add another wrinkle
- Paul, Paula, Paulina, paul, Paul, JeanPaul
- var myRegExp /\bPaul\b/gi
20Grouping Regular Expressions
- Use () to group characters into patterns that we
then apply repetition characters to - grouping regular expressions.htm
- var myString "JavaScript, VBScript and Perl"
- var myRegExp /\b(VB)?(Java)?Script\b/gi
21Grouping Regular Expressions
- Matching repeating characters
- A small hypothetical situation
- 009,007,001,002,004,003 is Valid
- 007,007,001,002,002,003 is not
- \d
- \d,
- \d,700
- (\d),\1
- repeating character.htm
22The split() Method
- Allows us to split a string into various pieces
with the split being made at the character or
characters specified as a parameter - Simple
- apple, banana, peach, orange
- What about
- apple, 0.99, banana, 0.50, peach, 0.25, orange,
0.75 - Split and retrieve just the names of the fruits
- a-z
- ch8_examp3.htm example
23The replace() Method
- Inserting into a string using replace()
- 1999, 2000, 2001
- To
- the year 1999, the year 2000, the year 2002
- We can refer to groups of character using n
where n refers to the position of the group - var myRegExp /\d4/g This finds each group
of 4 digits - mystring.replace(myRegExp, the year 1)
24The replace() Method
- Another example
- Replace single quotes with double quotes
- Distinguish single quotes from apostrophe
- Hello World said Mr. OConnerly.
- He then said My Name is OConnerly, yes thats
right, OConnerly - // will not work
- /\B\B/g will
- ch8_examp4.htm
25The search() Method
- Allows you to search a string for a pattern of
characters. If found, return is the position at
which it was found, else it returns 1 - Finds first occurrence, the /g directive is
irrelevant - ch8_examp5.htm
26The match() Method
- Similar to search except it returns an array with
the matches - match example 1.htm
- The years were 1999, 2000, and 2001
- Extract just the years
- myRegExp /\bd4\b/g
- A complex example
- ch8_examp5.htm