-5

I am trying to find a way around using spaces and hyphens in a javascript statement.

<script type='text/javascript'>
<!--
var {word} = '{word}';

if(
    {word} == 'hellothere' ||
    {word} == 'hello there' ||
    {word} == 'hello-there'
){ 
    document.write('blah blah'); 
}
else {  
    document.write('');
}
</script>

The above code is designed so that whenever the word is hello there a certain thing is displayed. However, it doesn't work when there is a space between the words: hello there. I tried using a hyphen: hello-there but that doesn't work either.

It only works when I write as one word: hellothere or HelloThere for better reading.

Why is this? And are there ways around it?

Quintero
  • 1
  • 1
  • 2
    `{word}`? Please post actual syntactically correct code. – Matt Burland Jun 25 '14 at 16:46
  • 2
    Your code doesn't make any sense. What is `{word}` other than nonsense? – Andy Jun 25 '14 at 16:46
  • Because variable names can't contain spaces of hyphens. It seems like you are using the same string value as variable name, which just won't work. It looks like you want `'{word}' === ...` instead of `{word} === ...`. – Felix Kling Jun 25 '14 at 16:46
  • 1
    Please create a fiddle where `'hello there' == 'hello there'` does not return true. – TheNorthWes Jun 25 '14 at 16:47
  • @MattBurland /@Andy, `{word}` is my variable. It is not necessary to expand on it since it is not integral to the particular problem. It is a variable that works. – Quintero Jun 25 '14 at 16:48
  • 1
    @Quintero it obviously is not working. [Can you modify this to illustrate your problem?](http://jsfiddle.net/g3rUZ/) – TheNorthWes Jun 25 '14 at 16:49
  • @Quintero If all your assumptions were correct, your code would work and you wouldn't be here. Consider showing the expanded code. – Paul Roub Jun 25 '14 at 16:49
  • @quintero: No, `var foo bar = 'foo bar';` can't work. – Felix Kling Jun 25 '14 at 16:49
  • @Andy `{word}` the variable is part of a bigger script that would only serve to clutter this segmented problem. All you need to know is that it works fine as described above. – Quintero Jun 25 '14 at 16:50
  • @FelixKling, Why can't I utilize spaces? Is it just prevented in Javascript IF statements? – Quintero Jun 25 '14 at 16:52
  • 1
    It's prevented in variable names. As Felix said, explicitly, in his first sentence. – Paul Roub Jun 25 '14 at 16:52
  • I'm assuming that every occurrence of `{word}` above is replaced with he same character sequence. So if you have `foo bar`, you end up with `var foo bar` which is simply invalid. The space denotes the end of the variable name. Similarly for `foo-bar`. That's not related to `if` statements at all, that's how variable names work in JS. Not every character is allowed in a variable name. – Felix Kling Jun 25 '14 at 16:54
  • 1
    So OP what you're saying is you want `var foo bar = 'foo bar';`? Whats the point of that? Just do `var word = 'foo bar'`. – Andy Jun 25 '14 at 16:56
  • @Andy, the code is not 'nonsense'. `{word}` is a variable used within a greater context which works fine on my server where the script is located but it is something that I cannot replicate in a fiddle. It is not necessary to know the functionality of `{word}` for this problem. It works fine when not using spaces or hyphens. Surely this would lead you to think that it wasn't 'nonsense'. My problem is with spaces and hyphens. – Quintero Jun 25 '14 at 16:56
  • 2
    When you posted this question it was nonsense. Only you supplying some context has made it less so. But only just. Consider that you only have a `javascript` tag, and there's no mention of _server_ anywhere in the post. – Andy Jun 25 '14 at 16:59
  • @Andy, because `{word}` is not the same every time. It is determined by user input much like a tag. So when the tag (`{word}`) is a certain string it produces a certain content. – Quintero Jun 25 '14 at 17:00
  • 1
    Bottom line: you can't directly use `{word}` as variable name because not every string or user input is a valid variable name. Either sanitize it or don't use it at all. There is nothing else to discuss about it. – Felix Kling Jun 25 '14 at 17:03
  • 2
    Let me spell it out for you: you post some JavaScript code that included `{word}`. `{word}` is a nonsense construct in JavaScript. It was only until you made some more comments did the question start to make sense. Oh, and @FelixKling is smarter than me. – Andy Jun 25 '14 at 17:07
  • 3
    @Quintero at this point, literally everyone commenting here sees the problem, except (apparently) you. Andy understands what you're saying the script does, but given the problems in the original code and question, he's reluctant to just take your word for it. Please consider looking at the explanations provided (summarized with a solution in my answer below). – Paul Roub Jun 25 '14 at 17:07
  • 1
    @Quintero: You'll find you get much more help and learn a lot more if you stop being combative towards people who are trying to help you. If people ask you to clarify your question, it's because it's not clear. You posted code tagged as javascript which isn't (valid) javascript. People would rather you clarify what you mean than just guess and you'll get better and more relevant answers if you clarify your question. – Matt Burland Jun 25 '14 at 17:11
  • @Andy, I should have mentioned in the OQ that `{word}` was a local variable but I guess I thought people would assume it, since it is an obvious error in syntax. However, I did mention that is my first comment. – Quintero Jun 25 '14 at 17:13
  • No. People were going to assume it was an error in syntax. – Andy Jun 25 '14 at 17:14
  • @PaulRoub, I think Felix answered best when he said you cannot use spaces or hyphens in variables. Where this entire thing escalated is the confusion around the local variable which I've had to try and explain. – Quintero Jun 25 '14 at 17:15
  • 1
    @Quintero: But in your comment you claimed that `{word}` was "not integral", however, it absolutely is. A lot of confusion could have been avoided if you'd simply filled in the real substitution for `{word}`. It helps to keep an open mind when asking questions and not just dismiss people who ask for clarification because you are sure it isn't relevant. – Matt Burland Jun 25 '14 at 17:16
  • @MattBurland, I haven't been 'combative' towards people. If you read all of the comments chronologically I could not be said to instigating any kind of blunt remark first. Yes, people could have phrased things in a politer way, but I wasn't being any more combative in my responses than the comments themselves. I tried to clarify in my first comment about the `{word}` variable, but even at that point it had hints that it could become a blunt exchange. – Quintero Jun 25 '14 at 17:19
  • @Quintero: You might find this answer worth reading: http://stackoverflow.com/a/9337047/1250301 – Matt Burland Jun 25 '14 at 17:20

2 Answers2

2

Assuming (since you won't show it) that {word} is expanded to hello there, your code becomes:

var hello there = 'hello there';

if(
  hello there == 'hellothere' ||
  hello there == 'hello there' ||
  hello there == 'hello-there'
){ 
  document.write('blah blah'); 
}
else {  
  document.write('');
}

As several of the commenters have pointed out, variables can't have spaces in their names.

Why do you need to name the variable after its contents? This version would work fine:

var myword = '{word}';

if(
    myword == 'hellothere' ||
    myword == 'hello there' ||
    myword == 'hello-there'
){ 

You could then rewrite your if to take spaces and dashes into account:

if (myword.match(/^hello[ \-]*there$/i)) {
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • There is no space in the variable name. `{word}` doesn't change. It is a variable used in a greater context which works on my server. – Quintero Jun 25 '14 at 17:06
  • No, by the time it actually runs in the browser, it's been replaced by `hello there`, according to your comments above. If that's not true, then (a) `{word}` is not a valid variable name and (b) `'{word}'` will never match `"hello there"` or anything else in the list. – Paul Roub Jun 25 '14 at 17:08
0

I think you're looking for

if ({word}.replace(/-| /g, "") == 'hellothere') { … }

(despite the obvious syntax issue). It just removes all hyphens and spaces from the {word} before comparing it. If you want to make it case-insensitive, add .toLowerCase().

Bergi
  • 630,263
  • 148
  • 957
  • 1,375