14
LING 408/508: Programming for Linguists Lecture 14 October 19 th

LING 408/508: Programming for Linguists Lecture 14 October 19 th

Embed Size (px)

Citation preview

Page 1: LING 408/508: Programming for Linguists Lecture 14 October 19 th

LING 408/508: Programming for Linguists

Lecture 14October 19th

Page 2: LING 408/508: Programming for Linguists Lecture 14 October 19 th

Last Time

• Added bells and whistles to the BMI calculator:– http://dingo.sbs.arizona.edu/~sandiway/ling508-15/bmi-gauge.html

Page 3: LING 408/508: Programming for Linguists Lecture 14 October 19 th

Javascript regular expressions• Let's write our own browser-based tester to help you learn regular

expressions:

form

http://dingo.sbs.arizona.edu/~sandiway/ling508-15/re-test.html

Page 4: LING 408/508: Programming for Linguists Lecture 14 October 19 th

Javascript regular expressions• Let's write our own browser-based tester to help you learn regular

expressions:

What Javascript provides:• RegEx object

– var re = new RegEx(string, flags)

– var re = /[A-Z]([a-z])*/gi (g= global; i=ignore case)• Methods:

– var a = string.match(re) returns an array• [entire match, ...submatches…]

– var a = regex.exec(string) returns an array– different behaviors (under global flag)

optional

Page 5: LING 408/508: Programming for Linguists Lecture 14 October 19 th

Javascript Regexp Tester

<body> <form> String: <input type=text name=str size=30> <br> Regex: <input type=text name=re size=30> Global match (g): <input type=checkbox name=g value=g> <input type=button value="Click" onclick="f(this)"> </form> <script> document.getElementsByTagName("form")[0].re.value = "Mr\. ([A-Z][a-z]*)" </script> <div id="output"></div> </body>

Page 6: LING 408/508: Programming for Linguists Lecture 14 October 19 th

Javascript Regexp Tester

function f(e) {var o = document.getElementById("output");o.innerHTML = "";var re_s = e.form.re.value;var s = e.form.str.value;if (re_s != "") { var flag_s = ""; if (e.form.g.checked) {

flag_s += "g" }

var regex = new RegExp(re_s,flag_s); if (e.form.g.checked) {

var a;while (a = regex.exec(s)) { o.innerHTML += a.toString() + "<br>"}

} else{ o.innerHTML = s.match(regex).toString()

}}

}

<input name=str>

<input name=re>

<input name=g>

Page 7: LING 408/508: Programming for Linguists Lecture 14 October 19 th

Javascript Regexp Tester

• Let's try the code:– http://dingo.sbs.arizona.edu/~sandiway/ling508-1

5/re-test.htmlMr. ([A-Z][a-z]*)

Mr\. ([A-Z][a-z]*)

Page 8: LING 408/508: Programming for Linguists Lecture 14 October 19 th

Javascript Regexp Tester

• Useful property– regex.lastIndex

Page 9: LING 408/508: Programming for Linguists Lecture 14 October 19 th

Regular expression syntax

http://www.w3schools.com/jsref/jsref_obj_regexp.asp

Page 10: LING 408/508: Programming for Linguists Lecture 14 October 19 th

Regular expression syntax

Page 11: LING 408/508: Programming for Linguists Lecture 14 October 19 th

Regular expression syntax

Page 12: LING 408/508: Programming for Linguists Lecture 14 October 19 th

Regular expression syntax

Page 13: LING 408/508: Programming for Linguists Lecture 14 October 19 th

Regular expression syntax

Page 14: LING 408/508: Programming for Linguists Lecture 14 October 19 th

Replace

• We'll also need the method replace():• var regex = new RegExp(re_s,flag_s);• modified_string = string.replace(regex,replacement)

– replacement string can contain $n – (n = group number)

developer.mozilla.org