如何在Vanilla JS中的特定字符之前保留字符串中的所有字母?

时间:2018-09-23 17:38:48

标签: javascript html

高级编码器,

我无法找到一种方法来将suffix-class中的option的{​​{1}}插入变量中需要隐藏的select中:

<option class="filteroption1 at-show" value="at-pei">
  [LOAD TRANSLATE East New Brunswick/P.E.I.]
</option>
<option class="filteroption1 at-show" value="at-nflnd">
  [LOAD TRANSLATE Newfoundland]
</option>
<option class="filteroption1 at-show" value="at-nova">
  [LOAD TRANSLATE Nova Scotia]
</option>
<option class="filteroption1 at-show" value="at-nflnd">
  [LOAD TRANSLATE West New Brunswick]
</option>
<option class="filteroption1 for-show" value="for-hk">
  Hong Kong
</option>
<option class="filteroption1 for-show" value="for-int">
  International
</option>
<option class="filteroption1 ont-show" value="ont-dur">
  Durham / Northumberland
</option>
<option class="filteroption1 ont-show" value="ont-ham">
  Hamilton
</option>
<option class="filteroption1 ont-show" value="ont-lond">
  London & District
</option>
<option class="filteroption1 ont-show" value="ont-ncapsel">
  [LOAD TRANSLATE National Capital Section]
</option>
<option class="filteroption1 ont-show" value="ont-nbay">
  [LOAD TRANSLATE North Bay]
</option>
<option class="filteroption1 ont-show" value="ont-nwo">
  [LOAD TRANSLATE Northwestern Ontario]
</option>
<option class="filteroption1 ont-show" value="ont-tor">
  Toronto
</option>
<option class="filteroption1 prairie-show" value="prairie-man">
  Manitoba
</option>
<option class="filteroption1 prairie-show" value="prairie-sask">
  Saskatoon
</option>
<option class="filteroption1 prairie-show" value="prairie-man">
  [LOAD TRANSLATE South Saskatchewan (Interim)]
</option>
<option class="filteroption1 qc-show" value="qc-mtl">
  [LOAD TRANSLATE Montreal]
</option>
<option class="filteroption1 qc-show" value="qc-qc">
  [LOAD TRANSLATE Quebec]
</option>
<option class="filteroption1 qc-show" value="qc-sher">
  Sherbrooke
</option>
<option class="filteroption1 west-show" value="west-calg">
  Calgary
</option>
<option class="filteroption1 west-show" value="west-edm">
  Edmonton
</option>
<option class="filteroption1 west-show" value="west-van">
  Vancouver
</option>
<option class="filteroption1 west-show" value="west-vanIsl">
  [LOAD TRANSLATE Vancouver Island]
</option>

E.G。需要检查字符串是否以ontprairie开头。我了解这是与substring相同的原理,但它是opposite。我想知道如何轻松地做到这一点(是的,即使在Pure Vanilla JS中也是如此),只保留字符串-之前的部分。

感谢您的所有答复,每个答案都是一个很好的答案! ;)

3 个答案:

答案 0 :(得分:2)

我能想到的最简单的方法就是在-上拆分字符串并获取数组的第一个元素:

const testString = 'This is a test-This should not show up'
console.log(testString.split('-')[0])

编辑:如果您希望将其应用于字符串中的多个单词,则可以使用Regex。

const stringTest = 'this-atasd is a test-aasodma to remove-slkdfmnlksdf suffixes-asuindkajsd'
const processedString = stringTest.replace(/-(?=.*)\w+/gi, '')
console.log(processedString)

答案 1 :(得分:2)

如果您:

  

需要检查字符串是否以ont或prairie开头。

然后,您可以简单地使用RegExp.test()进行精确测试:

let rx = RegExp(/^(prairie-|ont-)/)  // start with prairie- or ont-

console.log(rx.test("prairie-man"))         // true
console.log(rx.test("ont-nbay"))            // true
console.log(rx.test("SomeOtherString"))     // false
console.log(rx.test("SomeOtherString-ont-")) // false

答案 2 :(得分:1)

有几种方法可以提取某个字符之前的字符串的一部分。

一种方法是在该字符上分割字符串并返回第一部分:

function splitAndReturn(str, c) {
  return str.split(c)[0];
}

console.log(
  splitAndReturn('abcd', 'c') // 'ab'
)

另一种选择是使用正则表达式匹配“字符之前的所有字符”:

function matchEverythingBeforeChar(str, c) {
  return str.match(new RegExp(`(.+)${c}`))[1]
}

console.log(
  matchEverythingBeforeChar('abcd', 'c') // 'ab'
)

另一种选择(最好是作为学习练习)可能是通过循环自己实现功能。循环将遍历每个积累它们的字符,直到找到您要寻找的字符,然后返回积累:

function loopAndFind(str, c) {
  let acc = '';
  for (let i = 0; i < str.length; i++) {
    if (str[i] === c) {
      return acc;
    }
    acc += str[i];
  }
  return acc;
}

console.log(
  loopAndFind('abcd', 'c') // 'ab'
)

JavaScript还为我们提供了实现此累积功能的快捷方法:

function loopAndFindWithNativeMethods(str, c) {
  return str.substring(0, str.indexOf(c))
}

console.log(
  loopAndFindWithNativeMethods('abcd', 'c') // 'ab'
)