Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "LING 408/508: Programming for Linguists Lecture 14 October 19 th."— Presentation transcript:

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

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

3 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

4 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

5 Javascript Regexp Tester String: Regex: Global match (g): document.getElementsByTagName("form")[0].re.value = "Mr\. ([A-Z][a-z]*)"

6 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() + " " } } else{ o.innerHTML = s.match(regex).toString() } } }

7 Javascript Regexp Tester Let's try the code: – http://dingo.sbs.arizona.edu/~sandiway/ling508- 15/re-test.html http://dingo.sbs.arizona.edu/~sandiway/ling508- 15/re-test.html Mr. ([A-Z][a-z]*) Mr\. ([A-Z][a-z]*)

8 Javascript Regexp Tester Useful property – regex.lastIndex

9 Regular expression syntax http://www.w3schools.com/jsref/jsref_obj_regexp.asp

10 Regular expression syntax

11

12

13

14 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


Download ppt "LING 408/508: Programming for Linguists Lecture 14 October 19 th."

Similar presentations


Ads by Google