0

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? 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.

Filip Blaauw
  • 731
  • 2
  • 16
  • 29

3 Answers3

4

You can use String.prototype.trimEnd to remove whitespace from the end, and then check that:

function (string) {
  if (string.endsWith(':')) {
    // ends with :
  }
  else if (string.trimEnd().endsWith(':')) {
   // ends with : and white space
  }
  else {
    // does not end with :
  }
}
Steve
  • 10,435
  • 15
  • 21
  • In Python 3.9: AttributeError: 'str' object has no attribute 'trimEnd' – Timo Nov 11 '20 at 12:39
  • 1
    Hi @Timo, this question is for JavaScript. Instead of `trimEnd`, it seems like you can use `rstrip` for Python. See https://stackoverflow.com/a/2372578/6184972 – Steve Nov 11 '20 at 15:25
1

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. If, however, you want to test against more complex strings, you can also consider using Regular Expressions (also known as RegEx). That Mozilla documentation has an excellent tutorial on how to use regular expressions for 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:

/     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.

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. If you need to do complex pattern matching where JavaScript functions won't cut it, regular expressions can do the trick. It's worth reading up about and another good tool to put in the toolbox.

homersimpson
  • 4,124
  • 4
  • 29
  • 39
-1

Hello u might wanna use regex /(:\s*)/
s* will match 0 or all spaces if they exist

andy94
  • 69
  • 1
  • 2
  • 5