Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Are there additional quotation marks in Php, and JavaScript besides "..." and '...' in the case I need to nest them?
(The Alt+Number would be useful)
For example a Php echo:
echo "
onClick='changeImage('example.jpg');'
";
I know I could escape the quotation marks , but I am wondering if there are another "level" of them that can be nested.
Javascript
You are in luck, in ES2015 specs, javascript now allows template literals using back ticks.
console.log(`hello "world!" I'm doing well :)`);
PHP
Use Nowdocs. Don't use Heredocs unless you want to evaluate php code inside the string.
Note: someone already answered this, but I'll just reiterate to contain a whole solution.
echo <<<'STR'
`hello "world!" I'm doing well :)`
STR;
Don't use Heredocs
echo <<<STR
`hello "world!" I'm doing well :)`
STR;
Or
echo <<<"STR"
`hello "world!" I'm doing well :)`
STR;
Difference is the single quoted name. This is a Heredoc. It will evaluate PHP code denoted by ${expression} in your string.
Don't use back ticks
In PHP backticks will be evaluated as a shell command and return the output of said shell command.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a string that contains both html tags and plain text
for example it might be: <h1>Hello World</h1><p>Welcome to Javascript</p>
I want to extract html tags (only the tags without class names or attributes) to array or list, for example:
tags = ['<h1>', '</h1>', '<p>', </p>']
how to achieve this using Javascript ?
Notice that this could should run on the server so I don't have access to the DOM and so.
const regex = /<\/?[\w\d]+>/gi;
This should get tags with opening and closing tags, now let's go through why it works:
< is just the starting angle bracket
\/ is to match literal backslashes (like closing tags)
? to make the backslash 'optional'
[\w\d] for matching alphanumeric characters
+ to match more alphanumeric characters
> for the other angle bracket
Flags:
g to match all matches (heh)
i to be case insensitive (since HTML is case insensitive)
You need to use match() with the global flag
tags = html.match(/<[^>]*?>/g)
As pointed out in the comments, this answer won't work if you have a greater than sign (>) inside of an HTML tag which would still be valid HTML.
You can't reliably (or possibly at all) parse arbitrary HTML with regular expressions. (See here for why).
If you're running JavaScript on a server then presumably you're running Node.js. If so, get yourself an HTML parser library and use that to parse the HTML into a representation of the DOM. Then you can reliably extract all the tags from that.
There are a number of libraries available that might be suitable. You could try node-html-parser*, or search Google for nodeJS HTML parser for other options
*(no affiliation, not making recommendation or otherwise)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Could someone with more regex experience than me help me out?
return path.replace(/\//g, '.').replace(/^\./, '');
I have found this regex in a js file within a giant app. The JS when run through npm node-minify or any of the others sees it as a comment and turns it into this:
return path.replace(/\g, '.').replace(/^\./, '');
I get the first bit is replacing all \ with a . and the second bit trims any leading . from the string. Can i change this so the regex pattern is wrapped in quotes?
Just use the RegExp constructor and quote your pattern.
const path = '/usr/bin/env';
const matchSlash = new RegExp('/', 'g');
const translate = path => path.replace(matchSlash, '.').replace(/^\./, '');
console.log(translate(path));
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 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 7 years ago.
Improve this question
I am trying to be able to use replace(searchvalue, newvalue); and a user input that would be the search term.
An example would be replace(input,"example text");
But what I want is to be able to have the search term, but instead of replacing the search term, replace the space in front of it.
Ex. is the sentence: "Hi, I am using js to create this!"
and user inputs "js" replace(input, "html and ");
but instead of replacing "js", replace the space in front. So the output sentence would be:
"Hi, I am using html and js to create this!"
Would there be anyway to do this with replace?
You can use a function to handle the replacement in string.replace.
var newString = 'Hi, I am using js to create this!'.replace('js', function(match) {
return 'html and ' + match;
});
console.log(newString); // ... using html and js ...
In the above example I prepend "html and " to the variable match (which has the value of "js"). There is a lot of flexibility when you use a regex instead of a string to find the match.
MDN
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(