Truncates and removes numbers from a string.

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.