Break out from JavaScript variable - javascript

So, I have the following code:
<script type="text/javascript">
var name = prompt("What's your name?");
var greeting = "Hello " + name + " :D";
console.log(greeting);
</script>
I am trying to break out from name variable. For example: answering the question by
"alert(1);\\
In browser console, printing name variable shows the following:
Now, when trying to create a second variable with the same content it produces an error.
Why does "name" contain invalid syntax content? and is there any way to break out from the variable in this scenario?

When you input "alert(1);\\ in the prompt, the special characters are automatically escaped and the input is assigned to name, but when you are doing that via the console or a script, you have to escape the characters manually. Since you wrote the string using double-quotes, the correct way to do the assignment would be:
var test = "\"alert(1);\\\\";
When uing single-quotes, you wouldn't have to escape the double-quotes:
var test = '"alert(1);\\\\';
What I did here is escape the required characters by putting a \ before them. What you see when you try to view the value of name is the actual content without the escaping.

JavaScript believes that you are ending the string literal at the second quotation mark.
Two possible solutions:
Wrap with single quotes: test = '"alert(1)\\'
Escape the second quote: test = "\"alert//"

Related

Check first character of a word in a string begins with #

A few days ago I posted a similar question, but I do not quite understand the principle. Are there good resources where the replace function combined with regular expressions is explained?
Anyways, right now I have the following problem: A string which starts with # should be placed in an link. So #test should be replaced to #test .
Also, these rules should apply:
The string can only contain one #, which is at the beginning.
If there are more strings, also replace them. I thought you can do this by putting /g behind the regex?
This is what I have so far:
value = "is #test";
var text = value.replace(/^.*(#)(\w+).*$/, "<a href='$2'>$1$2</a>");
My output
#test
EDIT:
The link is now working. However, the word "is" is missing.
You need to capture the ambient text:
value = "is #test or what";
var text = value.replace(/^(.*)#(\w+)(.*)$/, "$1<a href='$2'>#$2</a>$3");
Or just capture less:
var text = value.replace(/#(\w+)/, "<a href='$1'>#$1</a>");
When performing a .replace(), you need to include all the characters in the RegExp that you wish to replace, preserving the ones you want to keep with parentheses.
var test = 'is #test';
function makeAt(string){
return string.replace(/^.*(#)(\w+).*$/, "<a href='$2'>$1$2</a>");
}
console.log(makeAt(test));

store ":;',./<>?,./asdaA12 in javascript variable dynamicaly (Unexpected token error)

I am tying to store ":;',./<>?,./asdaA12 value in js variable which is in gsp page but it could'nt store it properly and gives me run-time error because of special symbol contain " and ' as string.
my snap code is :
var editGrp_billingCode_val ="${returnVal?.groupCommand?.billingCode?:billingCode?:billingCode}";
Here Billing code populate with : ":;',./<>?,./asdaA12 value which contain " or ' symbols so for that page gives me run-time js error
is there any one solution for converting this types of string into String variable and store into js variable. like toString method in java
Value ":;',./<>?,./asdaA12 is not fixed it will come dynamically while running the application so i have to convert it at run-time as stored in js variable
I've figured it out, just enclose your variable (symbols) in between the "forward slash" e.g
<script>
var x = /".'*:",/
alert(x)
// this should print ".'*:",
</script>
By escaping them:
<script>
var x = "\".'*:\","
alert(x)
// this should print ".'*:",
</script>
Short escaping list:
" = \" or ' = \'
\ = \\
New Line = \n
Tab = \t
more: w3schools - JavaScript Strings

Regex with multiple start and end characters that must be the same

I would like to be able to search for strings inside a special tag in a string in JavaScript. Strings in JavaScript can start with either " or ' character.
Here an example to illustrate what I want to do. My custom tag is called <my-tag. My regex is /('|")*?<my-tag>((.|\n)[^"']*?)<\/my-tag>*?('|")/g. I use this regex pattern on the following strings:
var a = '<my-tag>Hello World</my-tag>'; //is found as expected
var b = "<my-tag>Hello World" + '</my-tag>'; //is NOT found, this is good!
var c = "<my-tag>Hello World</my-tag>"; //is found as expected
var d = '<my-tag>something "special"</my-tag>'; //here the " char causes a problem
var e = "<my-tag>something 'special'</my-tag>"; //here the " char causes a problem
It works well with a and also c where it finds the tag with the containing text. It also does not find the text in b which is what I want. But in case d and e the tag with content is not found due to the occurrence of the " and ' character. What I want is a regex where inside the tag " is allowed if the string is start with ', and vice versa.
Is it possible to achieve this with one regex, or is the only thing I can do is to work with two separate regex expressions like
/(")*?<my-tag>((.|\n)[^']*?)<\/my-tag>*?(")/g and /(')*?<my-tag>((.|\n)[^"]*?)<\/my-tag>*?(')/g ?
It's not pretty, but I think this would work:
/("<my-tag>((.|\n)[^"]*?)<\/my-tag>"|'<my-tag>((.|\n)[^']*?)<\/my-tag>')/g
You should be able to use de match from the first match ('|") and reuse it for the second match. Something like the following:
/('|")<my-tag>.*?<\/my-tag>\1/g
This should make sure to match the same character at the beginning and the end.
But you really shouldn't use regex for parsing HTML.

regular expression not matching on javascript but matches on other languages

so I've been running around regexp for a while now, and been using RegEx101 to test my patterns, and it never failed (yet).
So I am trying to replace android Emojicons strings to their appropriate HTML image tage via regex, the code seems to match without an issue in the site above, and even works with PHP, but somehow, it doesn't match at all in javascript... so here is my code:
function loadEmojisInMessage(message) {
var regExp = /({emoji:(.*?)})/g; //var regExp = new RegExp("({emoji:(.*?)})","g");
message.replace(regExp, '<img src="emojis/emoji_$2.png" id="$2" class="emojicon" />').toString();
return message;
}
at first I thought I am doing something wrong, so I changed the code to this, just for testing
function loadEmojisInMessage(message) {
var regExp = /({emoji:(.*?)})/g; //var regExp = new RegExp("({emoji:(.*?)})","g");
message.replace(regExp, 'test?').toString();
return message;
}
but even this does not replace at all! (my thought is that it is having an issue matching the pattern in the string :/ )
example strings to match :
{emoji:em_1f50f}
What I am trying to do here is replace the entire string (above) with image HTML tag, while using the second match [it is the second bracket () ] for the URL string
Best Regards
UPDATE :
I forgot to add first matching bracket, sorry!
Also, you can test the pattern here
You're not assigning the result of the replace() method call back to the variable message. If you don't to this, message remains unchanged.
message = message.replace(regExp, '<img src="emojis/emoji_$2.png" id="$2" class="emojicon" />');

Javascript regex not containing keyword with backslashes

I'm having a problem with a javascript regex that has to comment out all tags inside a script tag. But it can not comment out special first script tag with id "ignorescript".
Here is a sample string to regex:
<script id="ignorescript">
var test = '<script>test<\/script>;
var xxxx = 'x';
</script>
Script tag inside ignorescipt has extra backslash because it is JSON encoded (from PHP).
And here is the final result i have to get:
<script id="ignorescript">
var test = '<!ignore-- <script>test<\/script> ignore-->;
var xxxx = 'x';
</script>
Following example works:
content = content.replace(/(<script>.*<\\\/script>)/g,
"<!--ignore $1 ignore-->");
But I need to check that it does not contain a keyword "ignorescript". If that keyword comes up then I do not want to replace anything. Otherwise add ignore comments to whole script tag So far I have gotten this far:
content = content.replace(/(<script.((?!ignorescript).)*<\/script>)/g,
"<!--ignore $1 ignore-->");
It kinda works, but not the way it supposed to be. I also have one more backslash in ending tag. So I changed it to:
content = content.replace(/(<script.((?!ignorescript).)*<\\\/script>)/g,
"<!--ignore $1 ignore-->");
Not it does not find anything at all.
Got it finally working.
Here is the working regex:
/(<script(?!\sid="ignorescript").*?<\\\/script>)/g

Categories

Resources