Truncates and removes numbers from a string.
A query condition is required in the recent project. The data in a column in the table is displayed in the drop-down box to facilitate filtering and query. The data in this group is a combination of Chinese characters and numbers, therefore, I did a stupid way to remove the numbers in it. The method is as follows:
Html:
<! DOCTYPE html>
Js:
<Script> $ (function () {var oldStr = ["test code-1002-210", "Production Workshop code-203042-2 ", "Production Workshop code-204242-51", "Production Workshop code-203042-4", "No.-134", "No.-1244", "No.-11124 ", "Country Code-22-45632-1", "Country Code-22-45632-2"]; var newSelect = []; var newArr = []; var newObj = {}; var newOp = "";
// Use regular expressions to find the badge of a number, and then extract the Chinese character for (var I = 0; I <oldStr. length; I ++) {var num = oldStr [I]. search (/[0-9]/ig)-1; var newStr = oldStr [I]. substring (0, num); newSelect. push (newStr);} // traverses the new array containing only Chinese characters and deduplication for (var j = 0; j <newSelect. length; j ++) {if (! NewObj [newSelect [j]) {newArr. push (newSelect [j]); newObj [newSelect [j] = 1 ;}}
// Cyclically generated labels are added to the page element for (var k = 0; k <newArr. length; k ++) {newOp + = "<option>" + newArr [k] + "</option>" }$ ("# state" ).html (newOp );}); </script>