How to pass a single quote around a javascript variable? [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
I have a javascript code as shown below in which I want to pass a single quote around a javascript variable attribute.
Problem Statement:
I am wondering what changes I need to make at Line A so that I am able to pass a javascript variable in a single quote.
At Line A, I did this `'${attribute}'` but I am getting an error Uncaught SyntaxError: missing ) after argument list.

It looks like you're trying to use a template literal, but have over-complicated it:
confirm(`Are you sure you want to delete '${attribute}' ?`)
Note the use of back-ticks around the whole template literal, which syntactically allows you to use any quotes you like within the literal itself.

You need to put the whole string inside backticks and then you don't have to 'escape' your single quotes anymore.
confirm(`Are you sure you want to delete '${attribute}' ?`)

Related

Uncaught SyntaxError: expected expression, got ';' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
var popupContent = 'Click me!';
I'm currently using Leaflet and want a popup with clickable text, which will run some Javascript to define the variables stopno and nuscode. However, in the GeoJSON data feature.properties.nuscode is a string and not an integer, and thus I need to use quote marks to define the variable nuscode.
However, if I were to use quote marks again it would cause issues as I am using inline JS in the onclick property for the <a> tag, and using quote marks causes "Unexpected end of input" errors. How do I remedy this?
As of ES2015, you could use backticks to create what is known as template literals (template strings).
You can encapsulate text in backticks and interpolate JavaScript so that your code is much neater. It also helps avoid any conflicting use of single/double quotation marks, and avoids having to escape (\) strings.
More can be read about it here on MDN
const popupContent = `Click me!`;

JSON.stringify converts colon inside string to a unicode character [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I have a string object in javascript as given below:
time : "YYYY-MM-DDT00:00:00.000Z#YYYY-MM-DDT23:59:59.999Z"
When I use JSON.stringify to convert the object to string, I get the following string
"time=YYYY-MM-DDT00%3A00%3A00.000Z%40YYYY-MM-DDT23%3A59%3A59.999Z"
Here 2 characters, i.e., # and : are being converted to their unicode, which is unwanted behaviour.
How can I prevent this unwanted conversion, so that my string remains unchanged?
I can't reproduce your problem, can you show the complete code where the error occurs.
Also, try doing this conversion on the browser console and see what the result comes out to be.

Is it possible to make a regex that accepts ANY substring [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm somewhat new to regex. I understand most of the basics but what I'm trying to do is beyond my knowledge, and may not even be possible.
I'm trying to make a regex in JavaScript that can match a series of function calls in the following pattern.
Name.Name(Params).Name(Params)
The names could be any standard java function name. I understand how do to this part. The params though can be different number of parameters (Currently only 0-2)
My biggest issue however is that params could potentially take ANY string with either a single or double quotation mark, or variable names. I have added some examples below as I need all of these to work with my regular expression (if Possible).
Examples:
Func.Foo().Bar()
Foo.Bar('foo', bar).Foobar()
Foo.Bar("foo", "bar").bar(')')
Foo.Bar('/"foo/"').bar("foo(bar/")")
My main concern here is I cant just look for a opening and parentheses or even 2 quotation marks.
Is it possible to use a regex so that I can parse the function call and parameters out?
The short answer to the Question in the title is yes, you can build a regex that matches any substring. But unfortunately that is not what you want. If you allow arbitrary substrings your regex will either match many cases you dont want to match or it will become extremely complex (see the email regex for an example).
What you want is a tokenizer!(https://medium.freecodecamp.org/how-to-build-a-math-expression-tokenizer-using-javascript-3638d4e5fbe9)
Edit: for the solutions in the comments: the ast parser is for java, the author wants to use javascript.

Access JSON Object data with space in name [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I make an API call and get returned some JSON. I then parse this JSON using
var json = $.parseJSON(result);
To get to the level I need to within this json object, I do something like this
console.log(json.data[0].value)
That will print something like the following to the console
Option "1166325"
Option Two "3329076"
So if I do
console.log(json.data[0].value.Option)
I am printed out 1166325. If however I do
console.log(json.data[0].value.Option Two)
I get an error SyntaxError: missing ) after argument list. I have also tried
console.log(json.data[0].value.['Option Two'])
But this returns SyntaxError: missing name after . operator
So how can I access this data considering there is a space in the name?
Thanks
Yes, just lose the . character:
console.log(json.data[0].value['Option Two'])
This is known as bracket notation and can always be used to access a property. Dot notation can only be used when the property name is a valid Javascript identifier. This means (a) letters and numbers; (b) _ underscores; (c) $ dollar signs.

My simple Regex doesn't work [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
I have this simple regex that is supposed to match any numbers and '+' signs
/^[\d\+]+$/g
What it does is this:
1 => true
11 => false
11+11 => true
1+1 => false
It's driving me nuts!
(I'm using JavaScript, if it matters)
Some assumptions I did when reproducing your error:
You're using the test()-method of the RegExp-prototype, not the match()-method of the String-prototype.
Your pattern is stored in a variable and you reuse it over multiple calls to the test()-method.
At a first glance, the result is somewhat unexpected, but I'll try to explain what is happening.
Your RegExp has the global-Flag set to true. This causes subsequent calls to the test()-method to advance past previous matches, as stated here. This essentially means that after your first regular expression is evaluated and a match was found, the index of this match is stored into the RegExp-object and the next match will start at that very index, omitting some characters at the beginning. For a deeper explanation, I'd recommend reading this thread.
This is not really what you want, right? My quick recommendation would be to simply remove the global-flag, as you don't really need it from my point of view. If you want to ensure that your regular expression is only matching full strings rather than substrings, use the ^and $ metacharacters (as you already did).
EDIT:
If you really need the global-flag though, try to use the match()-method of the String-prototype, as it does not advance past previous matches. Instead it uses the advancing feature and captures all matches, resetting the index afterwards.
var pattern = /^[\d\+]+$/g;
"1".match(pattern); // => true
"11+11".match(pattern); // => true
"1+1abc".match(pattern); // => false

Categories

Resources