LING 408/508: Programming for Linguists Lecture 14 October 19 th.
-
Upload
jean-bryant -
Category
Documents
-
view
235 -
download
0
Transcript of LING 408/508: Programming for Linguists Lecture 14 October 19 th.
![Page 1: LING 408/508: Programming for Linguists Lecture 14 October 19 th.](https://reader034.fdocuments.us/reader034/viewer/2022050802/5697bfd41a28abf838cacfb4/html5/thumbnails/1.jpg)
LING 408/508: Programming for Linguists
Lecture 14October 19th
![Page 2: LING 408/508: Programming for Linguists Lecture 14 October 19 th.](https://reader034.fdocuments.us/reader034/viewer/2022050802/5697bfd41a28abf838cacfb4/html5/thumbnails/2.jpg)
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.](https://reader034.fdocuments.us/reader034/viewer/2022050802/5697bfd41a28abf838cacfb4/html5/thumbnails/3.jpg)
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.](https://reader034.fdocuments.us/reader034/viewer/2022050802/5697bfd41a28abf838cacfb4/html5/thumbnails/4.jpg)
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.](https://reader034.fdocuments.us/reader034/viewer/2022050802/5697bfd41a28abf838cacfb4/html5/thumbnails/5.jpg)
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.](https://reader034.fdocuments.us/reader034/viewer/2022050802/5697bfd41a28abf838cacfb4/html5/thumbnails/6.jpg)
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.](https://reader034.fdocuments.us/reader034/viewer/2022050802/5697bfd41a28abf838cacfb4/html5/thumbnails/7.jpg)
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.](https://reader034.fdocuments.us/reader034/viewer/2022050802/5697bfd41a28abf838cacfb4/html5/thumbnails/8.jpg)
Javascript Regexp Tester
• Useful property– regex.lastIndex
![Page 9: LING 408/508: Programming for Linguists Lecture 14 October 19 th.](https://reader034.fdocuments.us/reader034/viewer/2022050802/5697bfd41a28abf838cacfb4/html5/thumbnails/9.jpg)
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.](https://reader034.fdocuments.us/reader034/viewer/2022050802/5697bfd41a28abf838cacfb4/html5/thumbnails/10.jpg)
Regular expression syntax
![Page 11: LING 408/508: Programming for Linguists Lecture 14 October 19 th.](https://reader034.fdocuments.us/reader034/viewer/2022050802/5697bfd41a28abf838cacfb4/html5/thumbnails/11.jpg)
Regular expression syntax
![Page 12: LING 408/508: Programming for Linguists Lecture 14 October 19 th.](https://reader034.fdocuments.us/reader034/viewer/2022050802/5697bfd41a28abf838cacfb4/html5/thumbnails/12.jpg)
Regular expression syntax
![Page 13: LING 408/508: Programming for Linguists Lecture 14 October 19 th.](https://reader034.fdocuments.us/reader034/viewer/2022050802/5697bfd41a28abf838cacfb4/html5/thumbnails/13.jpg)
Regular expression syntax
![Page 14: LING 408/508: Programming for Linguists Lecture 14 October 19 th.](https://reader034.fdocuments.us/reader034/viewer/2022050802/5697bfd41a28abf838cacfb4/html5/thumbnails/14.jpg)
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