inconsistent behaviour in jQuery selector [duplicate] - javascript

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 \\.

Related

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"/

Strip tags from a string - HTML5 [duplicate]

This question already has answers here:
Remove HTML Tags in Javascript with Regex
(13 answers)
Closed 7 years ago.
The following string is valid HTML in HTM5, even with the > in the attribute:
'<span src="whatever.png" data-info="hello>there">text</span>';
Almost all solutions proposed before use a variant of:
replace( /<.*?>/g, '' )
Which fails for this due to the > in the attribute.
There is also the option of writing the content to a dummy element and then reading back the textContent, but that is horribly slow when dealing with a lot of data.
So, I was wondering if anyone has any suggestions? Removing the attributes - or at least the attribute values with the matching quotes in a first pass regex and then a second pass to strip the tags seems like it might be the way to go, but I can't quite get my head around it!
I believe this regex should work for your purpose:
/<([^\"\'>]*|\"([^"\\]|\\.)*\"|\'([^'\\]|\\.)*\')*>/g
Essentially, it matches any character other than ", ' or >, or it matches " or ' followed by any characters followed by another " or '.
Just use it in the same way as you did in the original question - string.replace(/<([^\"\'>]*|\"([^"\\]|\\.)*\"|\'([^'\\]|\\.)*\')*>/g, '');

Javascript / jQuery: How to convert string to HTML character code [duplicate]

This question already has answers here:
How to convert characters to HTML entities using plain JavaScript
(12 answers)
Closed 8 years ago.
I have a string that contains text including special characters like apostrophes, quotes, ampersand etc. that I would like to convert to HTML character codes, e.g. to show &#39 instead of an apostrophe etc. (as per the following list: http://www.w3.org/MarkUp/html-spec/html-spec_13.html.
I do not necessarily need to convert letters and spaces as long as the above and some other basic characters are encoded correctly.
For a temporary workaround I use simple replaces like str = str.replace("'", "&#39") but would like to avoid this as it only covers certain characters.
I was thinking of encodeURI or encodeURIcomponent but would like to know what is the best and easiest way to achieve this without encoding more than necessary.
Can someone tell what is the best way here if the idea is to avoid any issues with other JS where this is inserted dynamically, esp. thinking of single quotes, double quotes and any other character that could cause issues here (referring to JavaScript / jQuery and English language only) ?
function encodeEntities(value) {
return $('<div />').text(value).html();
}

Can single quotes and double quotes be used interchangeably while enclosing strings in javascript? [duplicate]

This question already has answers here:
Are double and single quotes interchangeable in JavaScript?
(23 answers)
Closed 8 years ago.
Till now whatever programming languages I have encountered use double quotes for string and single quotes for characters but some of the javascript code that I saw seemed to be using them interchangeably,is it possible?
In which situations can they be interchanged?
In which situations they CANNOT be interchanged?
They are interchangeable, but when you open a string with one, you must close it with the same character. For instance,
"Hello' would not be a valid string.
EDIT: PS, this allows for you to enclose quotes in a string. For example, if you want to have the string literal "Hello", you could have
var string = '"Hello"'
yes they can as long as you keep in mind a few main things;
1- double quotes are the preferred by the browser to se they parameters values, so you never use them when setting javascript values or function calls within the DOM.
2- double quotes are also used to specify json an properties a json string using single quotes to delimiter they key, value pairs is not valid.
3-they don't complement each other, so what one starts one must end.
4 - if you need to use the same quote inside your string you need to scape it.

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.

Categories