這次帶給大家正規如何對輸入內容進行處理,正則對輸入內容進行處理的注意事項有哪些,下面就是實戰案例,一起來看一下。
該小腳本實現的是將輸入框內容中取出標籤中的src中的內容,然後在原來位置進行替換內容,然後在移除輸入內容中的html標籤,空格等操作
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <script> window.onload = function () { var target = document.getElementById("p1"); var but = document.getElementById("but"); var value; but.onclick = function () { value = target.innerHTML; result = dealText(value); alert(result); }; }; function dealText(msg) { var text1 = imageRound(msg); var text2 = removeTag(text1); var text3 = removeBlank(text2); var text4 = changeNBSP(text3); return text4; } function DealNum(num) { var content = []; for (var i = 0; i < num.length; i++) { // content.push(defaultSrc(num[i])); } return content; } function defaultSrc(src) { return ':avator:'; } function returnValue(srcValue) { var num = []; var pattern = /([^\/]+)\./; var src; for (var i = 0; i < srcValue.length; i++) { src = pattern.exec(srcValue[i])[1]; num.push(src); } // alert(num); var source = DealNum(num); return source; } function imageRound(text) { var reg2 = /<img.*?\>/gi; var s = text.match(reg2); var num = returnValue(s); var i = 0; var content = text.replace(reg2, function () { return num[i++]; }); return content; } function removeTag(text) { var text1; var reg = /<.*?>|<.*?\/>/g; text1 = text.replace(reg, ""); return text1; } function removeBlank(text) { var text1; var reg = /\s+/g; text1 = text.replace(reg, ""); return text1; } function changeNBSP(text) { var text1; var reg = / /g; var reg1 = /\;/g; text1 = text.replace(reg, ' '); text1 = text1.replace(reg1, ""); return text1; } </script> </head> <body> <p contenteditable="true" id="p1">abc efg <img src="asdjk/123.jpg"/><span>hi gk</span><img src="ahdkh/124.jpg"/> <p>lmnop</p> q rst uvw </p> <button id="but">更新</button> </body> </html>
相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!
推薦閱讀:
#以上是正規如何對輸入內容進行處理的詳細內容。更多資訊請關注PHP中文網其他相關文章!