regex for stripping javascript on PHP [duplicate] - javascript

This question already has answers here:
remove script tag from HTML content
(13 answers)
Closed 7 years ago.
I want to delete all javascript found on a string with PHP, I'm trying to do it trough preg_replace like this: $text = preg_replace("/<script.*<\/script>/mis", "", $text);
But after doing this, $text is an empty string. What I'm doing wrong? Trying this regex on http://www.phpliveregex.com/ it seems to work, but using it in real life returns me nothing.

Typically you do not just want to remove (escape) javascript but all sorts of HTML tags which prevent a vulnerability to you. You can do so using
http://docs.php.net/manual/en/function.htmlspecialchars.php
or
http://docs.php.net/manual/en/function.htmlentities.php
Then you dont have to bother with regex altogether.

Related

Is there a feature in javascript such as deleting words starting with "<" and ending with ">"? [duplicate]

This question already has answers here:
Parse an HTML string with JS
(15 answers)
Cross-Browser Javascript XML Parsing
(3 answers)
Parse XML using JavaScript [duplicate]
(2 answers)
Trying to use the DOMParser with node js
(8 answers)
Closed 2 years ago.
Is there a feature in javascript such as deleting words starting with "<" and ending with ">"?
For example I want to extract html codes from this object
"description": "<mainText><stats><attention> 25</attention> Hareket Hızı</stats></mainText><br>",
Sounds like a job for regex.
I think this could do it:
// BAD CODE
function useRegex(input) {
let regex = /^<[a-zA-Z]+> <[a-zA-Z]+><[a-zA-Z]+>$/i;
return regex.test(input);
}
EDIT: seems like my idiot self has messed this one up. Don't use regex, as commented by someone else.
I personally hate regex, so here are some tools to help you
Website to help you get started
https://regexr.com/
And how to apply that in Javascript
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Regex code creator. It's like magic
https://regex-generator.olafneumann.org/

Get text between tags regex javascript [duplicate]

This question already has answers here:
Regular expression to get a string between two strings in Javascript
(13 answers)
Closed 4 years ago.
I am trying to get the text between the two tags which also handles multiple lines.I have managed to do it in PHP regex but i am stuck on the javascript one.
<any_tag>random text
dffdffdfdfdfdfdfdfd
dfdfdfdfdfdfdfdfdf
</any_tag>
I want to get the text between <any_tag> and </any_tag>
This is the regex for php
https://regex101.com/r/tQ1bX7/2
Now i am trying to achieve to same in javascript
Give try on following :
/[^(<any_tag>)](?:.*(\r?\n?).*)*(?=\<\/any_tag\>)/gi
Explination :
[^(<any_tag>)] match a single character not present in the list below
(<any_tag>) a single character in the list (<any_tg>) literally (case insensitive)

JavaScript variable containing line breaks [duplicate]

This question already has answers here:
Pass a PHP string to a JavaScript variable (and escape newlines) [duplicate]
(14 answers)
Closed 8 years ago.
I have text containing line breaks from a TextArea stored in a database. A php script tries to retrieve this text and store it in a JavaScript variable. The problem is that the string is not properly understood by JavaScript and it is interpreted that JavaScript variable is not correctly ending because JavaScript assumes a line break.
Any idea how could I save text containing line breaks in a JavaScript variable?
Thanks in advance.
Any idea how could I save text containing line breaks in a JavaScript
variable?
Yes, Use PHP's json_encode:
var myvar = <?php echo json_encode($myVarValue); ?>;

replace asterisk innerhtml javascript [duplicate]

This question already has answers here:
How do I replace an asterisk in Javascript using replace()?
(6 answers)
Closed 8 years ago.
how do i replace the asterisks with a blank ("") in the innerhtml using javascript?
i have tried this.
document.getElementById("lab").innerHTML = document.getElementById("lab").innerHTML.replace(/*/g, '');
and i also tried doing the asterisk itself but it gets commented..
searched everywhere i can't seem to find ways to remove the appended in innerhtml aside from replacing it with a blank.. are there any other options?
i also tried searching for ways to use replace and other options. for added info i am using this for the validation of a form
thanks in advance!
You have to escape the asterisk:
.....replace(/\*/g, "")
because it is a special character in regular expressions.

Swap + (plus) with space in string [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I decode a URL with jQuery?
Could somebody please help me to take a string (i.e. URL parameter) and remove the +(plus) or %20 to a regular space (" ")?
For example, to change this:
"my+string+to+change" or "my%20string%20to%20change"
to this:
"my string to change"
I use a lot of jQuery already, so that would be fine. Or pure javascript if that's easier.
Use decodeURIcomponent(stringToDecode)
Note this is not a jquery feature but a regular javascript function.

Categories