-2

I want to check an URL whether it it's last character is '\'(back slash)

Ex: http://randomurl/withbackslash\

I am able to get the last character using URL.slice(-1), but not able to compare it inside if statement.

if (lastcharacter === '\') {
  return
}

this condition is not working

CherryDT
  • 25,571
  • 5
  • 49
  • 74
  • 1
    what code did you try? – Md Junaid Alam Apr 15 '20 at 14:59
  • 1
    Does this answer your question? [How can I get last characters of a string](https://stackoverflow.com/questions/5873810/how-can-i-get-last-characters-of-a-string) – 0stone0 Apr 15 '20 at 15:00
  • 2
    `if (url.endsWith('\\')) console.log('It ends with a backslash')` - [docs](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith) - The double `\ ` is needed because `\ ` is also an escape character in strings, so it needs to be escaped by itself to be taken literally. – CherryDT Apr 15 '20 at 15:01
  • 2
    Didn't you already ask this question [here](https://stackoverflow.com/q/61225741/2679750) ? – Mark C. Apr 15 '20 at 15:01
  • 1
    Another easy way to get the last character of a string: `string.slice(-1)` – lucas Apr 15 '20 at 15:02
  • 1
    I see now, the issue is not how to get the last character, but actually it's about not knowing how to escape a backslash. It should be reopened or closed with a _different_ duplicate link! – CherryDT Apr 15 '20 at 15:05
  • @venkateswarlu puttamsetti: You need to write `'\\'` instead of `'\'`, see [this](https://www.w3schools.com/js/js_strings.asp) and scroll down to "Escape Character" – CherryDT Apr 15 '20 at 15:06
  • @CherryDT: I don't know about this previously, your solution is working fine. Thank you so much – venkateswarlu puttamsetti Apr 15 '20 at 15:20
  • @CherryDT Agreed. Voting to reopen since the reason for closing is inappropriate. – skomisa Apr 15 '20 at 17:42

1 Answers1

2

The code '\' is not a valid Javascript code. I doubt that the problem you describe is that "this condition is not working". You probably got an error like this:

SyntaxError: Invalid or unexpected token

The reason is that the character \ is not understood to be the literal character "backwards slash" because it is used in Javascript as the "escape character" - it is never understood as is and instead causes the next character to be interpreted as something else.

The sequence "\'" is understood in Javascript as "the literal single quote character" instead of "'" which is normally used to start and end single quoted strings.

So when Javascript sees '\' it sees a start of a single quoted string, followed by the literal "single quote" character, and then ... where's the end of the string?

What you probably meant to have instead is "a string that contains a single literal back slash character". You can do that by disabling the special meaning of \ by using another \ (escaping the escape character).

So your condition should look like this:

if (lastcharacter === '\\') {

BTW: the === operator is redundant here, and == could have been safely used instead.

Guss
  • 30,470
  • 17
  • 104
  • 128
  • About `===` vs `==` it depends on your coding style. The [JavaScript Standard Style](https://standardjs.com) for example mandates `===` here. (And since the remaining visible part of the OP's code matches the Standard style as well, this may very well be the reason.) – CherryDT Apr 15 '20 at 20:30
  • I was not familiar with "JavaScript Standard Style", and after reviewing it I take offence with that npm package using the term "standard Style" to describe itself - as their FAQ say - it's not actually a standard or endorsed in anyway by any official Javascript body. I needed to put it out there to make sure readers aren't confused by that intentionaly confusing and pretentious name. Also - I don't agree with using `===` there. – Guss Apr 15 '20 at 20:57
  • The identity operator should only be used when it may differ on result with the equality operator and the developer took a position on whether they want to have that difference - otherwise there's no reason for the equality operator - instead, just use `==` for identity (as languages like Java do) and use something else for the "looks similar enough" operator. – Guss Apr 15 '20 at 21:05
  • Right, I didn't mean to say that the "Standard" style is _the_ standard. It's just _a_ standard. Still, a lot of packages, people and companies use it nowadays. The logic behind using `===` wherever possible is explained [here](https://eslint.org/docs/rules/eqeqeq). I didn't want to start a lengthy discussion about taste here though, just to make that clear ^^ – CherryDT Apr 15 '20 at 22:46
  • I don't think it's a "taste" issue - definitely after reading the linked explanation: In real applications I have used `==` many times, on purpose to get the behaviour that eslint calls "obscure" (while linking to the official language specification!!!). That rule leaves no room for correctly using equality to identify when values are equal but not identical. To more clearly understand why they are wrong, note that they incorrectly use the term "type safe equality", then try `new String("a") === new String("a")`. Also, do read the spec. – Guss Apr 15 '20 at 23:05
  • The idea is that you should rather conciously convert the type to decide what sort of comparison you want. For example `String(x) === y` or `Number(x) === y` instead of `x == y` which may operate as either of the two. – CherryDT Apr 15 '20 at 23:08
  • @CherryDT that is quite verbose when you know what you are doing and write code that conveys that clearly and tersely - for example `if (userInput == 5)` . Obviously the user input is always a string, why would I need to manually convert it first when it's quite clear `==` will do it for me? – Guss Apr 15 '20 at 23:13
  • As I said, taste ;) So please let's leave it at that. It would take much more than this comment section to really get into the pro and contra, so it's the wrong place and was surely already discussed to oblivion elsewhere. I just wanted to mention that `==` may not be the ultimate right answer here (I neither said that `===` is, though, just that it may be the preferred option in some coding styles). – CherryDT Apr 15 '20 at 23:17