-2

I'm trying to to verify if a string contain this simbol \.

if (this.inputCity.includes('\')){
    // do something,,,
}

The problem is that backslash? is an escape? character... So how can I check if string contain \ ?

Thanks!

3 Answers3

0

You can escape the escape character by just adding another one:

if (this.inputCity.includes('\\')){
    // do something,,,
}
Collbrothers
  • 155
  • 1
  • 12
0

Because it's an escape character, you have to escape it with another backslash. :)

if (this.inputCity.includes('\\')){
    // do something,,,
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
andykanu
  • 36
  • 6
  • Thanks, there is a mode includes both of backslash? ( / and \ )? – Pietro Tamburini Jan 30 '21 at 12:40
  • the forward slash '/' isn't a special character so you can search for that as normal. If you want both in the same statement, it would be: `if (this.inputCity.includes('\\') || this.inputCity.includes('/')) {///do something }` – andykanu Jan 30 '21 at 12:44
0

I think you want to use the AND operator:

if(this.inputCity.includes('\\') && this.inputCity.includes('/') {
     // This will run if inputCity includes \ AND (hence the operator name) /
}
Collbrothers
  • 155
  • 1
  • 12