如何通过启动关键字删除字符串

时间:2018-02-08 20:49:45

标签: javascript jquery

我有一个时区选择列表,我只需要显示以关键字America开头的那些时区,然后删除剩下的https://jsfiddle.net/o1yrcnrm/

<select class="form-select">
    <option value="Africa/Abidjan">Africa/Abidjan: Thursday, February 8, 2018 - 20:45 +0000
    </option>
    <option value="Africa/Accra">Africa/Accra: Thursday, February 8, 2018 - 20:45 +0000
    </option>
</select>

我认为这应该起作用,如果启动关键字不是美国,那么它将被删除。

我目前正在关注Javascript how to remove text from a string

var ret = "data-123".replace('data-','');
console.log(ret);   //prints: 123

我是朝着正确的方向吗?

2 个答案:

答案 0 :(得分:3)

您只需 indexOf() 即可获得字符串&#34; America&#34;的索引位置在整个Result内。如果不是value,则0不会以&#34; America&#34;你应该删除节点。

JQuery过度 for something like this 。当它第一次出现时,DOM API并不像现在这样成熟,而且JQuery使一切变得简单。现在JavaScript和DOM API已经发展,JQuery可以为我们做的很多事情现在可以通过vanilla JavaScript轻松完成。 JQuery仍然有它的位置,但是对于比简单查询,字符串操作和DOM搜索更高级的操作。

&#13;
&#13;
value
&#13;
// Get all the <option> elements into an array
var values = Array.prototype.slice.call(document.querySelectorAll("option"));

// Loop over the array
values.forEach(function(opt){
  if(opt.value.indexOf("America") !== 0){  // If the value doesn't start with "America"
    opt.parentNode.removeChild(opt);   // Remove the <option> from the document
  }
});
&#13;
&#13;
&#13;

答案 1 :(得分:1)

replace仅修改单词,而不是删除整个项目,请尝试使用正则表达式或使用indexOfincludes方法。

var str = "America, ...";
var n = str.indexOf("America");
if(n !== -1){ //this means the 'America' is within the word }

这里有如何使用jQuery实现它:

&#13;
&#13;
$('.form-select option').each(function(i, item){
  if($(item).text().indexOf("America") !== 0){ 
    $(item).remove();
  }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="form-select">
  <option value="Africa/Abidjan">Africa/Abidjan: Thursday, February 8, 2018 - 20:45 +0000</option>
  <option value="Africa/Accra">Africa/Accra: Thursday, February 8, 2018 - 20:45 +0000</option>
  <option value="America/Argentina/Ushuaia">America/Argentina/Ushuaia: Thursday, February 8, 2018 - 17:45 -0300</option>
  <option value="America/Aruba">America/Aruba: Thursday, February 8, 2018 - 16:45 -0400</option>
  <option value="America/Asuncion">America/Asuncion: Thursday, February 8, 2018 - 17:45 -0300</option>
  <option value="America/Atikokan">America/Atikokan: Thursday, February 8, 2018 - 15:45 -0500</option>
  <option value="America/Bahia_Banderas">America/Bahia Banderas: Thursday, February 8, 2018 - 14:45 -0600</option>
  <option value="America/Bahia">America/Bahia: Thursday, February 8, 2018 - 17:45 -0300</option>
  <option value="America/Barbados">America/Barbados: Thursday, February 8, 2018 - 16:45 -0400</option>
  <option value="America/Belem">America/Belem: Thursday, February 8, 2018 - 17:45 -0300</option>
  <option value="America/Belize">America/Belize: Thursday, February 8, 2018 - 14:45 -0600</option>
  <option value="America/Blanc-Sablon">America/Blanc-Sablon: Thursday, February 8, 2018 - 16:45 -0400</option>
  <option value="America/Boa_Vista">America/Boa Vista: Thursday, February 8, 2018 - 16:45 -0400</option>
  <option value="Australia/Currie">Australia/Currie: Friday, February 9, 2018 - 07:45 +1100</option>
  <option value="Australia/Darwin">Australia/Darwin: Friday, February 9, 2018 - 06:15 +0930</option>
  <option value="Australia/Eucla">Australia/Eucla: Friday, February 9, 2018 - 05:30 +0845</option>
  <option value="Australia/Hobart">Australia/Hobart: Friday, February 9, 2018 - 07:45 +1100</option>
  <option value="Australia/Lindeman">Australia/Lindeman: Friday, February 9, 2018 - 06:45 +1000</option>
  <option value="Australia/Lord_Howe">Australia/Lord Howe: Friday, February 9, 2018 - 07:45 +1100</option>
  <option value="Australia/Melbourne">Australia/Melbourne: Friday, February 9, 2018 - 07:45 +1100</option>
</select>
&#13;
&#13;
&#13;

进一步信息:https://www.w3schools.com/jsref/jsref_indexof.asp https://www.w3schools.com/jsref/jsref_includes.asp