Replacing Text


var address = /(\w+), ([A-Z]{2}) (\d{5})/; var sams = "Indianapolis, IN 46240"; var result = sams.replace(address, "$3 $1, $2"); 

The replace() method every JavaScript string supports can replace text. It searches for a regular expression and replaces the match with another string. Within this replacement string, you can back-reference submatches. $0 points to the first match, $1 references the first submatch (within parentheses), $2 the second submatch, and so on. The preceding code searches for the city, state, and zip code elements and then rearranges them. The result is "46240 Indianapolis, IN".




JavaScript Phrasebook(c) Essential Code and Commands
JavaScript Phrasebook
ISBN: 0672328801
EAN: 2147483647
Year: 2006
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net