[英]Javascript: Split string by loop in loop
I've got string with IDs and Names separated by ^ (between ID and Names) and ;我有 ID 和名称的字符串,由 ^(在 ID 和名称之间)和 ; 分隔。 (between sets) for example
(组间)例如
var string = "1^John Smith;2^Sophia Williams;3^Emily Johnson;";
I need to get something like this我需要得到这样的东西
$('#1').html('<option value="1">John Smith</option><option value="2">Sophia Williams</option><option value="3">Emily Johnson</option>');
I tried loops but got stuck:我试过循环但卡住了:
var string = "1^John Smith;2^Sophia Williams;3^Emily Johnson;";
var a = string.split(";"),
i;
for (i = 0; i < a.length; i++){
if (a[i] !== ""){
var b = a[i].split("^"),
i2;
for (var i2 = 0; i2 < b.length; i++) {
var name = b[i2];
console.log(name);
}
}
}
Im not sure that it's good way我不确定这是个好方法
new Option(text, value, defaultSelected, selected)
var string = "1^John Smith;2^Sophia Williams;3^Emily Johnson;" var options = string.split(';').map(i => { return new Option(...i.split('^').reverse()) }) $('#1').html(options)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select id="1"></select>
You can build a HTML string inside your loop by grabbing the first element from b
as the value for your option and the second element from b
to be the text in your option tag.您可以通过将
b
的第一个元素作为选项的值,将b
的第二个元素作为选项标签中的文本,在循环中构建一个 HTML 字符串。 You can then add a string HTML version of the option tag using these text and value components to the accumulated string each iteration of your for-loop:然后,您可以使用这些文本和值组件将选项标记的字符串 HTML 版本添加到 for 循环每次迭代的累积字符串中:
var string = "1^John Smith;2^Sophia Williams;3^Emily Johnson;"; var a = string.split(";"); var html_str = ""; for (var i = 0; i < a.length-1; i++) { // loop to a.length-1 so you don't need an if-statement to check blanks var b = a[i].split("^"); var val = b[0]; var txt = b[1]; html_str += '<option value="' + val +'">' + txt +'</option>'; } $('#one').html(html_str); console.log(html_str);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select id="one"></select>
An alternative approach could be to use a regular expression to get the components from your string an convert it to your desired HTML string by using .replace()
with a replacement function:另一种方法是使用正则表达式从字符串中获取组件,并通过使用
.replace()
和替换函数将其转换为所需的 HTML 字符串:
var string = "1^John Smith;2^Sophia Williams;3^Emily Johnson;"; var html_str = string.replace(/(\\d+)\\^([^;]+);/g, (_, val, txt) => `<option value="${val}">${txt}</option>`); $('#one').html(html_str); console.log(html_str);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select id="one"> </select>
The above regular expression:上面的正则表达式:
(\\d+)\\^
: Groups any digits (group 1) which have a carat ^
following them (\\d+)\\^
: 将任何跟有克拉^
数字(第 1 组)分组([^;]+);
: Groups any characters which are not a semi-colon ;
;
(group 2), which are followed by a semi-colon. These groups are formed for each occurrence in your string, and then used in the .replace()
method's callback, where group 1 is val
and group 2 is txt
.这些组是为字符串中的每次出现而形成的,然后在
.replace()
方法的回调中使用,其中组 1 是val
,组 2 是txt
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.