TAILIEUCHUNG - Beginning JavaScript Third Edition phần 10

Tiếp theo, kể từ khi kiểm tra các plug-in hoặc điều khiển phụ thuộc vào trình duyệt, bạn kiểm tra xem nếu điều này là một trình duyệt của Microsoft. Nếu không, bạn cho rằng đó là Firefox, mặc dù cho một ví dụ thực tế cuộc sống, bạn có thể muốn làm kiểm tra chi tiết hơn. | Appendix A Exercise Solutions Well that would work but it would also replace the a in walked which you don t want. You want to replace the letter a but only where it s a word on its own and not inside another word. So when does a letter become a word The answer is when it s between two word boundaries. The word boundary is represented by the regular expression special character b so the regular expression becomes this var myRegExp ba b gi The gi at the end ensures a global case-insensitive search. Now with your regular expression created you can use it in the replace method s first parameter. myString myRegExp the Question 3 Imagine you have a website with a message board. Write a regular expression that would remove barred words. You can make up your own words. Solution html body script language JavaScript type text javascript var myRegExp sugar candy choc olate oholic gi var myString Mmm I love chocolate I m a chocoholic. I love candy too sweet sugar candy myString myRegExp salad alert myString script body html Save this as ch0 . For this example pretend you re creating script for a board on a dieting site where text relating to candy is barred and will be replaced with references to a much healthier option salad. The barred words are chocolate choc chocoholic sugar candy candy Let s see how you can build up the regular expression to remove the offending words. 688 Appendix A Exercise Solutions Start with the two basic words so to match choc or candy use this candy choc Next add the matching for sugar candy. Since the sugar bit is optional you group it by placing it in parentheses and adding the question mark after it. This means Match the group zero times or one time. sugar candy choc Finally you need to add the optional olate and oholic end bits. You add these as a group after the choc word and again make the group optional. You can match either of the endings in the group by using the character. sugar candy choc olate

TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.