當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
javascript里你绝对用的上的字符分割函数--原创
生活随笔
收集整理的這篇文章主要介紹了
javascript里你绝对用的上的字符分割函数--原创
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//?在數組內字符為未知情況下,合并和分割的解決方案
var data = [['your name', 'myvalue'], ['myr name', 'thivalue']];function string_join(data) {var str = '', arr = [];for (var i = 0; i < data.length; i++) {for (var j = 0; j < data[i].length; j++) {data[i][j] = slash(data[i][j]);}arr.push(data[i].join('/'));}return arr.join(','); }function slash(string) {return String(string).replace(/[\\/,]/g, '\\$&'); }function string_split(string) {var c,cur_str = '',cache = [],result = [];for (var i = 0; i < string.length; i++) {c = string.charAt(i);switch(c) {case '\\':cur_str += string.charAt(++i);break;case '/':cache.push(cur_str);cur_str = '';break;case ',':cache.push(cur_str);cur_str = '';result.push(cache);cache = [];break;default:cur_str += c;}}if (cur_str.length) {cache.push(cur_str);}if (cache.length) {result.push(cache);}return result; }var before = string_join(data); console.log(before); var after = string_split(before); console.log(after);
var data = [['your name', 'myvalue'], ['myr name', 'thivalue']];function string_join(data) {var str = '', arr = [];for (var i = 0; i < data.length; i++) {for (var j = 0; j < data[i].length; j++) {data[i][j] = slash(data[i][j]);}arr.push(data[i].join('/'));}return arr.join(','); }function slash(string) {return String(string).replace(/[\\/,]/g, '\\$&'); }function string_split(string) {var c,cur_str = '',cache = [],result = [];for (var i = 0; i < string.length; i++) {c = string.charAt(i);switch(c) {case '\\':cur_str += string.charAt(++i);break;case '/':cache.push(cur_str);cur_str = '';break;case ',':cache.push(cur_str);cur_str = '';result.push(cache);cache = [];break;default:cur_str += c;}}if (cur_str.length) {cache.push(cur_str);}if (cache.length) {result.push(cache);}return result; }var before = string_join(data); console.log(before); var after = string_split(before); console.log(after);
?個人原創,轉載請注明出處,?http://www.cnblogs.com/feng524822/p/3877779.html
轉載于:https://www.cnblogs.com/feng524822/p/3877779.html
總結
以上是生活随笔為你收集整理的javascript里你绝对用的上的字符分割函数--原创的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浏览器的垃圾回收机制
- 下一篇: win10怎么强制清除数据库 Win10