replace asterisk innerhtml javascript [duplicate] - javascript

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.

Related

inconsistent behaviour in jQuery selector [duplicate]

This question already has answers here:
How to select html nodes by ID with jquery when the id contains a dot?
(8 answers)
Closed 5 years ago.
$('#a.b') // doesn't work
document.getElementById('a.b') // works
$('[id="a.b"]') // works
Can anyone explain why $('#a.b') doesn't work.
Do not use meta characters in your Id's while using Jquery.
. is a meta character and selector see them as regex. You need to escape to make it work. For ex \\.
In this case -
$('#a\\.b')
Javascript getElementById also works because, for it, it's just a String.
When you giving as id="a.b" it checks for a value and won't get treated as meta character.
jQuery thinks it is a class. So, you write it using escape sequence as -
$('#a\\.b')
You should use the escaping operator as follows:
$('#a\\.b')
jQuery does not allow meta-characters as a literal part of a name, and those must be escaped with two backslashes \\.

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)

regex to retrieve specific url from text [duplicate]

This question already has answers here:
Regular Expression to find a string included between two characters while EXCLUDING the delimiters
(13 answers)
Closed 7 years ago.
An API I use returns this text:
<http://192.168.1.10:8080/longUrl>; rel="recording-session",
<http://192.168.1.10:8080/realLongUrl>; rel="h264-session-sdp",
<http://192.168.1.10:8080/realLongDifferentUrl>; rel="h264-session-sdp",
<rtp://239.1.1.18:5006>; rel="destination-high",
<rtp://239.1.1.17:5006>; rel="destination-low"
I'm trying to retrieve the first URL that is followed by ; rel="h264-session-sdp.
So in this case that would be: http://192.168.1.10:8080/realLongUrl
I've been fiddling around trying to modify examples found here on SO, but just can's seem to get it right.
try this one /([^<]+)(?:>; rel\=\"h264\-session\-sdp\")/
Selects the text inbetween the greater then and less then characters:
?<=\<)(.*?)(?=>)
https://regex101.com/r/oS5sX6/1
And if you wanted to select the urls on multiple lines, add the g and m modifiers:
/(?<=\<)(.*?)(?=>)/gm
https://regex101.com/r/oS5sX6/2
Try this:
/(?<=\<)(.*?)(?=\>)>; rel="h264-session-sdp"/

regex for stripping javascript on PHP [duplicate]

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.

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