简体   繁体   English

如何检查string.endsWith并忽略空格

[英]How to check for string.endsWith and ignore whitespace

I have this working function, to check if a string ends with a : 我有此工作功能,以检查字符串是否以:结尾

var string = "This is the text:"

function (string) {
  if (string.endsWith(':')) {
    // ends with :
  }
  if (string.endsWith(': ')) {
   // ends with : and a space
  }
  else {
    // does not end with :
  }
}

I also want to check if string ends with a colon followed by space, or even two spaces: :_ or :__ (where underscore represent spaces in this syntax). 我还想检查字符串是否以冒号结尾,后跟空格,甚至两个空格:_:__ (其中下划线表示此语法中的空格)。

Any ideas on how to implement this whithout using multiple if statements or defining every possible combination of colon and spaces? 关于如何使用多个if语句或定义冒号和空格的每种可能组合的任何想法? Let's assume there could be any number of spaces after the colon, but if last visible character is colon, I want to catch it in my function. 假设冒号后面可以有任意数量的空格,但是如果最后一个可见字符是冒号,我想在函数中捕获它。

You can use String.prototype.trimEnd to remove whitespace from the end, and then check that: 您可以使用String.prototype.trimEnd从末尾删除空格,然后检查:

function (string) {
  if (string.endsWith(':')) {
    // ends with :
  }
  else if (string.trimEnd().endsWith(':')) {
   // ends with : and white space
  }
  else {
    // does not end with :
  }
}

For your specific example, @Steve's answer will work just fine, because you're testing against a specific condition at the end of your string. 对于您的特定示例,@ Steve的答案将很好地起作用,因为您正在针对字符串末尾的特定条件进行测试。 If, however, you want to test against more complex strings, you can also consider using Regular Expressions (also known as RegEx). 但是,如果要针对更复杂的字符串进行测试,则还可以考虑使用正则表达式 (也称为RegEx)。 That Mozilla documentation has an excellent tutorial on how to use regular expressions for JavaScript. 该Mozilla文档中有关于如何对JavaScript使用正则表达式的出色教程。

To create a regex pattern and use it to test your string, you can do something like the following: 要创建一个正则表达式模式并将其用于测试您的字符串,您可以执行以下操作:

 const regex = /:\\s*$/; // All three will output 'true' console.log(regex.test('foo:')); console.log(regex.test('foo: ')); console.log(regex.test('foo: ')); // All three will output 'false' console.log(regex.test('foo')); console.log(regex.test(':foo')); console.log(regex.test(': foo')); 

...Where the regular expression /:\\s*$/ can be interpreted like so: ...其中正则表达式/:\\s*$/可以这样解释:

/     Start of regex pattern
 :    Match a literal colon (:)
 \s   Right afterward, match a whitespace character
   *  Match zero or more of the preceding characters (the space character)
 $    Match at the end of the string
/     End of regex pattern

You can use Regexr.com to do live testing for different regex patterns that you come up with, and you can input sample text into the text box to see if your pattern matches. 您可以使用Regexr.com对您提出的不同正则表达式模式进行实时测试,并且可以在文本框中输入示例文本以查看您的模式是否匹配。

Regular expressions are a powerful tool. 正则表达式是一个强大的工具。 There are some cases where you want to use them and other cases where it's overkill. 在某些情况下,您想使用它们,而在某些情况下,它会显得过大。 For your particular example, just using a simple .endsWith() is more straightforward and most likely preferred. 对于您的特定示例,仅使用简单的.endsWith()更直接,并且最有可能成为首选。 If you need to do complex pattern matching where JavaScript functions won't cut it, regular expressions can do the trick. 如果您需要执行复杂的模式匹配,而JavaScript函数不会削减它,则正则表达式可以解决问题。 It's worth reading up about and another good tool to put in the toolbox. 值得一读,并在工具箱中放置另一个好工具。

Hello u might wanna use regex /(:\\s*)/ 您好,您可能想使用正则表达式/(:\\ s *)/
s* will match 0 or all spaces if they exist s *将匹配0或所有空格(如果存在)

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

相关问题 string.endswith(“”)在IE中不起作用(不确定如何使用Polyfill) - string.endswith(“”) does not work in IE(not sure how to use a Polyfill) String.endsWith(/ [字符串] / [1-999] /) - String.endsWith(/[string]/[1-999]/) 如何在字符串上实现“EndsWith”? - How to implement a “EndsWith” on a string? 如何检查字符串是否包含字符和空格,而不仅仅是空格? - How can I check if string contains characters & whitespace, not just whitespace? 如何检查内部的所有数组元素 endwith() - How to Check All Array Element Inside endswith() 如何使用 JS endswith() 方法检查字符串变量是否以数组中的任何一项结尾? - How to use the JS endswith() method to check if a string variable ends with any one item from an array? 在Javascript / jQuery中,如何检查字符串的特定部分并确定它是空格还是字母? - in Javascript/jQuery, how to check a specific part of a string and determine if it is a whitespace or letter? 如何检查一个字符串是否包含任何字符JavaScript之间的空格? - How to check if a string contains any kid of whitespace between characters JavaScript? 如何检查字符串中除了字母和空格之外没有其他字符 - How to check that there are no other characters in the string except letters and whitespace regex - 忽略字符串开头的空格 - regex - ignore whitespace from the beginning of the string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM