Javascript Regular Expression


<html><head></head><body> <script>
var astr = "This information is confidential";
var apattern = new RegExp("info");
var result = apattern.test(astr);
var result2 = apattern.exec(astr);
document.write("apattern.test(astr): ",result,"<br>");
document.write("apattern.exec(astr): ",result2,"<br>");
</script></body></html>



Match, Search, Replace
<html><head></head><body> <script>
var astr = "HelloBraveNewWorld";
document.write(astr.match(/H/),"<br>");
document.write(astr.match(/l/g),"<br>");
document.write(astr.match(/b.*e/i),"<br>");
document.write(astr.search(/b.*e/i),"<br>");
document.write(astr.replace(/b.*e/i,''),"<br>");
document.write(astr.replace(/[A-Z]/g, "_$&"),"<br>");
</script></body></html>



Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου