Home > Article > Web Front-end > How regularity processes input content
This time I will show you how regular rules process input content, and what are the precautions for regular rules to process input content. The following is a practical case, let's take a look.
What this small script implements is to extract the content in the src in the tag from the input box content, then replace the content in the original position, and then remove thehtml tag in the input content. , spaces and other operations
<!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>I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:
Regular implementation in js to separate numbers with spaces every four digits
How to do it in JS Use regular regex to replace multiple spaces in a string with one space
The above is the detailed content of How regularity processes input content. For more information, please follow other related articles on the PHP Chinese website!