简体   繁体   English

通过…javascript / jquery分割字符串

[英]splitting a string by  …javascript/jquery

I'm using the bootstrap selectpicker and i need a delimiter to split out the text selected. 我正在使用引导程序选择器,并且我需要一个定界符来分割所选的文本。 The reason for this question is when a user selects multiple options in the bootstrap select, the text in not separated by a delimiter like the values: 出现此问题的原因是,当用户在引导程序选择中选择多个选项时,文本中的文本不由分隔符(如值)分隔:

   <select class="selectpicker" multiple>
       <option value=1>Mustard</option>
       <option value=2>Ketchup</option>
      <option value=3>Relish</option>
       </select>

The javascript to create the picker: 创建选择器的JavaScript:

   $('.selectpicker').selectpicker();

The user selects mustard and ketchup. 用户选择芥末酱和番茄酱。 To get the values: 要获取值:

   $('.selectpicker').val();//outputs '1,2'

Notice the comma delimiter. 注意逗号分隔符。 But i need the text as well: 但是我也需要文本:

  $('.selectpicker option:selected').text(); //outputs 'MustardKetchup

Notice no delimiter. 注意没有定界符。 So i added an invisible delimiter like so: 所以我添加了一个不可见的分隔符,如下所示:

    <option value=1>Mustard&nbsp;&nbsp;&nbsp;&nbsp;</option>
       <option value=2>Ketchup&nbsp;&nbsp;&nbsp;&nbsp;</option>
      <option value=3>Relish&nbsp;&nbsp;&nbsp;&nbsp;</option>

Now i get the following as expected: 现在,我得到了预期的结果:

   $('.selectpicker option:selected').text(); //outputs 'Mustard   Ketchup

But when i try to split the string, this doesn't work 但是当我尝试分割字符串时,这不起作用

  var text = $('.selectpicker option:selected').text(); //outputs 'Mustard   Ketchup
  var splitstr = text.split('    '); //does not split by 4 spaces only has an array of 1

Neither does this: 这也没有:

  var splitstr = text.split('&nbsp;&nbsp;&nbsp;&nbsp;'); //does not split by 4 spaces only has an array of 1
   //kinda figured this wouldnt work,but took a shot anyway

I chose 4 spaces because my data has commas in it, so a comma delimiter won't work. 我选择4个空格是因为我的数据中包含逗号,因此逗号定界符将不起作用。 How do i split a string by 4 spaces ( ) in javascript or jquery? 如何在javascript或jquery中将字符串除以4个空格()?

Thank you 谢谢

How do i split a string by 4 spaces ( ) in javascript or jquery? 如何在javascript或jquery中将字符串除以4个空格()?

Use regular expressions to split: 使用正则表达式进行拆分:

var splitstr = text.split(/\s{4}/);

Instead of splitting the string why not try extracting the separate strings into an array by iterating over $('.selectpicker option:selected') like this: 与其拆分字符串,不如尝试通过遍历$('。selectpicker option:selected')来将单独的字符串提取到数组中,如下所示:

var arr = [];
$('.selectpicker option:selected').each(function() {
    arr.push( $( this ).text() );
});

A non-breaking space isn't a space that we type. 不间断空格不是我们键入的空格。 It's String.fromCharCode(160) . 它是String.fromCharCode(160) You'll want to spit it with four of those. 您需要将其中的四个吐出来。 If you were doing $('.selectpicker option:selected').html() (which is alright since <option> elements are fairly limited to what elements they can contain), then you could use four &nbsp; 如果您正在执行$('.selectpicker option:selected').html() (这很好,因为<option>元素仅限于它们可以包含的元素),则可以使用四个&nbsp; strings as a delimiter. 字符串作为分隔符。

Here's what I recommend doing though: 不过,我建议您这样做:

var text = $('.selectpicker option:selected').text();
var nbsp = String.fromCharCode(160);
var splitstr = text.split(nbsp + nbsp + nbsp + nbsp);

You could do this without going through the trouble of adding space delimiters to your <option> values by looping through each selected option and adding the value to an array. 通过遍历每个选定的选项并将值添加到数组中,您可以避免在<option>值中添加空格分隔符的麻烦。

var selected = [];
$('.selectedpicker option:selected').each(function() {
    selected.push($(this).text());
}

[Edit] [编辑]

Toby T beat me to it. 托比T击败了我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM