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'm building my output text looping a JSON object using JavaScript. Everything is working fine until I come to the part where I need to add an ID number to an image URL. My images are stored in a database and I am using an ASHX handler to load the image however what I end up with is not exactly what I need.
My code that I need to end up with is ~/ImageHandler.ashx?id=35
but what I get is ~/ImageHandler.ashx?id='35'. Single quotes around the ID.
I know it is the syntax of " and "".
What I have tried is
myOutput += "<img src ='~/ImageHandler.ashx?id='" + ID + class='person-image'></img>"
and every combination that does not work.
If I remember correctly I need some combination of triple single quote or double quotes something or some combination of the both. ID is an integer that is being read from a JSON object.
Thanks in advance
You can add double quotes using the escape character \
myOutput += "<img src=\"~/ImageHandler.ashx?id=" + ID + "\" class=\"person-image\"></img>"
This will append
<img src="~/ImageHandler.ashx?id=35" class="person-image"></img>
Related
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 2 years ago.
Improve this question
How to replace the specific string only using one replace instead of two?
const formattedUrl = url.replace('flashget://', '').replace('&abc','')
What I have tried: (Not Working)
const formattedUrl = url.replace(/flashget:\/\/ | &abc/g, '').replace('&abc','')
Example
Input Url: flashget://W0ZMQVNIR0VUXWh0dHA6Ly93d3cuZm9yZWNlLm5ldC93aW43LnJhcltGTEFTSEdFVF0=&abc
Formatted Url: W0ZMQVNIR0VUXWh0dHA6Ly93d3cuZm9yZWNlLm5ldC93aW43LnJhcltGTEFTSEdFVF0=
Take out the spaces around the |
This is my attempt:
https://regex101.com/r/eFO7Eh/2
Search Regex:
flashget:\/\/(.*)\&.*$
Replace term:
$1
Just pay attention to the fact that this is a different logic and requires handling capture groups.
remove the space before and after the or |. it will work.
let url = "flashget://W0ZMQVNIR0VUXWh0dHA6Ly93d3cuZm9yZWNlLm5ldC93aW43LnJhcltGTEFTSEdFVF0=&abc"
const formattedUrl = url.replace(/flashget:\/\/|&abc/g, '');
console.log(formattedUrl);
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 3 years ago.
Improve this question
I need to find a regex to extract first occurrence of a string from a data.
For example my data is like :
SFASDŞŞVMA SAD SADAS MYABCD12345678911TY ISIADABCD12345678911SAD
I need to extract ABCD123456789 from this data.
I need to find first occurrence of string always starts with ABCD and has total length of 13.
How can I achieve this with using regex?
I tried regex /^ABCD(\w{9})$/gm which didn't work for me.
You can use /ABCD\w{9}/g with match() to get the result from first index:
var str = "SFASDŞŞVMA SAD SADAS MYABCD12345678911TY ISIADABCD12345678911SAD"
console.log(str.match(/ABCD\w{9}/g)[0])
The pattern that you tried ^ABCD(\w{9})$ does not match because you use anchors ^ and $ to assert the start and the end of the string.
Note that if you want a full match only, you don't need a capturing group (\w{9})
You can omit those anchors, and if you want a single match from the string you can also omit the /g global flag and the /m multiline flag.
ABCD\w{9}
Regex demo
const regex = /ABCD\w{9}/;
const str = `SFASDŞŞVMA SAD SADAS MYABCD12345678911TY ISIADABCD12345678911SAD`;
console.log(str.match(regex)[0])
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
While learning JavaScript I come across one issue. When I add multiple spaces between two strings, all spaces get converted to a single space. I tried to add multiple spaces between two strings with the help of concatenation operator(+). Also, I tried by adding multiple white spaces after the first string and before the second string but every time I get the same result. Two strings are separated by only one space in between them.
I searched for the cause but everyone is telling how to overcome this problem with the help of some regular expression and string replace function in JavaScript. I want the root cause of this problem.
I also know PHP. In PHP, this thing doesn't happen, PHP strings behave as expected by the developer. Whatever number of spaces I add in between, before or after the string they get added then why not in JavaScript?
Someone please explain me the cause and difference in this case between PHP and JavaScript.
I tried following expressions :
"John" + " " + "Doe";
"John " + " " + " Doe";
"John" + " " + " Doe";
"John Doe";
The output I got every time is : John Doe
The reason is not JavaScript, but HTML. The standard says that any number of whitespaces (spaces, linebreaks, etc ...) get reduced to a single space.
If you want to prevent this behavior, use .
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 3 years ago.
Improve this question
How to make JS popup window inside echo line?
I have this line but the popup not work :
echo '<td> Edit</td>';
then I check the link location on status bar :
javascript:popWin(edit.php?id=12)
as what I learn before it should be single quote inside the popWin(), but I have no clue to solve this..
To place a literal singlequote in your echo use backslash to escape it:
echo '<td> Edit</td>';
Just replace this echo for this:
echo "<td> Edit</td>";
When you use double quotes, you can use simple quotes inside the echo without escaping it. (\)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am confused why onclick is not wokring
Code1:
onclick="return clicked('35','http://www.google.com');"
Code2:
onclick='return clicked('35','http://www.google.com');'
If i used code1 onclick works fine but not with code2
thnx
Because you're breaking out of the onclick too soon, since you use single quotes for multiple things. The first one doesn't break since you can have single quotes in double quotes. If you use the second approach, escape the single quotes in the function call.
It's because of ' and " characters. If you open a " and want to put another string inside you need to use ' or it will close the first one.
That's why the code1 works, you start the string with ", then you use ' to specify parameters so the string isn't closed.
In code2, you start the string with ' and then use ' again to specify parameters. So the string you started is closed in the middle of you onclick statement.
code2 would work with :
onclick='return clicked("35","http://www.google.com");'
If you use single quotes for HTML attributes, you need to use double quotes for the Javascript arguments:
onclick='return clicked("35","http://www.google.com");'
Otherwise the browser is confused and thinks the attribute value is only return clicked(