JQuery implements the method for adding specific content to a string according to the specified length. jquery string
This example describes how jQuery adds a specified length to a string. Share it with you for your reference. The specific analysis is as follows:
In a recent project, mobile phone numbers must be separated by identifiers for easy reading. After searching for a mobile phone number on the Internet and finding no proper code, I wrote a function by myself, you can insert delimiters to a string based on the specified length.
Var split_str = false; function insert_flg (str, flg, sn) {str = str. replace (new RegExp (flg, "g"), ""); var newstr = ""; var tmp; var len = str. length; // length var num = len/sn; // number of segments var start; var end; // len % sn // whether the complete segments are 0: yes for (I = 0; I <num; I + = 1) {if (len % sn! = 0) {// incomplete segment start = I * sn-1; end = I * sn + (sn-1);} else {start = I * sn; end = (I + 1) * sn;} start = start <0? 0: start; if (end <= len) {tmp = str. substring (start, end);} newstr + = (end> = len )? Tmp: tmp + flg;} split_str = newstr; return newstr ;}$ (function () {var phone =$ ("# phone"); phone. blur (function () {// triggers var cont = phone when the focus is lost. val (); cont = jQuery. trim (cont); var str_p = '-'; // shard symbol var s = 4; // if (! Cont | split_str = cont) return false; // when the focus leaves again, check whether the content has changed. phone. val (insert_flg (cont, str_p, s ));})})
I hope this article will help you with jQuery programming.