I'm using this regex
<a [^>]*href[ ]*=[ ]*\"|'[^>]\"|'[^>]*>
to search in example string:
idhasidhioashdoihas <a onclick=alert('blablabla') href='www.hello.com'
onclick=alert('blablabla') > asdfsgdufisdugfusdg
It should match
<a onclick=alert('blablabla') href='www.hello.com'onclick=alert('blablabla') >
but it only matches
'blablabla') href='www.hello.com' onclick=alert('blablabla') >
Any idea where is the problem?
Your | is in the wrong place:
<a [^>]*href[ ]*=[ ]*\"|'[^>]\"|'[^>]*> is effectively:
<a [^>]*href[ ]*=[ ]*\" or '[^>]\" or '[^>]*>
If you want to mark " or ' in this exact place use []:
<a [^>]*href\s*=\s*["'][^>]*["'][^>]*>
Example:
a = "idhasidhioashdoihas <a onclick=alert('blablabla') href='www.hello.com' onclick=alert('blablabla') > asdfsgdufisdugfusdg";
a.match(/<a [^>]*href\s*=\s*["'][^>]*["'][^>]*>/)
["<a onclick=alert('blablabla') href='www.hello.com' onclick=alert('blablabla') >"]
You don't correctly test for the two possible attribute value delimiters. You can use this one :
/<a [^>]*href[ ]*=[ ]*[\"']?[^>][\"']?[^>]*>/
I just changed \"|' to [\"']? (note that it's possible not to have quotes at all, hence the ?)
The character classes you use are not always appropriate and you must surround your alternation by a group (ie: (?:'|")), but you don't need it. You can try this, with the same idea:
<a (?:[^h>]+|h(?!ref))*\bhref\s*=\s*["'][^"']*["'][^>]*>
But if you want only to find a link tag, you can use <a.+?> as thg435 suggests it.
(Note that the href value is not always between quotes:
<a (?:[^h>]+|h(?!ref))*\bhref\s*=\s*(?:["'][^"']*["']|[^\s>]*)[^>]*>
(or to be sure to have the same quotes)
<a (?:[^h>]+|h(?!ref))*\bhref\s*=\s*(?:(["'])(?:\\\1|[^"']+|(?!\1)["'])*\1|[^\s>]*)[^>]*>
Related
I want this number "*3*123#" to be inserted as the tel number.
but upon clicking link from android devices the # sign omitted and the number to be dialed is *3*123.I have also tried all of these codes but none of them could do the job.
<a href="tel:*3*123#">
<a href="tel:*3*123#">
try this code
<a href="tel:*3*123%23" >Call us!</a>
You can achieve this without JS, by using'#', which is the equivalent to & => & for the # symbol.
I.e:
tel:*3*123#
I have this HTML tag <a href="abc> google </a>, how can i put the " after the letter c using regular expression
HTML: <a href="abc> google </a>
Regex: /\=\s*["“'”](.*?)[^“"'”]\s*\>/g
Replace: ="$1">
https://regex101.com/r/1FQods/1
https://jsfiddle.net/liev04/6n038nvm/
How about
str.replace(/href="[^\s>"]+/, function(match) { return match+'"' });
I suggest the following:
str.replace(/=\s*["']([^"']*?)\s*?(?=>)/g,'="\1"');
This should work also in cases where the second " exists already. It also allows for blanks between the = and the beginning of the string.
See here for a demo: https://regex101.com/r/xo52ka/1
Another issue might be cases like:
<a href="abc def > google </a>
My Solution will turn that into
google
But, of course, this solution has its limitations and is by far not watertight. It will only work on the last attribute of each tag (because of the lookahead (?=>) in the regexp).
This code is working fine. But, I just want to remove variable url in third line and write direct www.google.com. Need corrected syntax of below code please. Quotes are so messy! I know there is just a little mistake. But didn't figure out.
website: function() {
var url = 'www.google.com';
this.echo('<a onclick="openHTTP(\''+url+'\')" href=""> My Website </a>', {raw:true});
You'll want to only replace the delimiting quotations (those ending and starting string literals) as well as +url+.
this.echo('<a onclick="openHTTP(\'www.google.com\')" href=""> My Website </a>', {raw:true});
Noting that your current snippet concatenates 2 literals with the variable:
'<a onclick="openHTTP(\''
'\')" href=""> My Website </a>'
The escaped quotations should be kept for the client-side code. They'll allow the browser to understand www.google.com as a string literal. The \ will be removed by the parser, so the output includes:
<a onclick="openHTTP('www.google.com')" href="">
website: function() {
this.echo('<a onclick="openHTTP(\'http://www.google.com\')" href=""> My Website </a>', {raw:true})
}
please try this one
just replace "url" variable with "www.google.com"
this.echo('<a onclick="openHTTP('www.google.com')" href=""> My Website </a>', {raw:true});
This is my html:
<div class="profile_nav_tabs" id="profile_nav_tabs">
<ul class="profile-tabs">
<li>
<a data-toggle="tab" href="#tab1">Tab 1</a>
</li>
<li>
<a data-toggle="tab" href="#tab2">Tab 2</a>
</li>
<li>
<a data-toggle="tab" href="#tab3">Tab 3</a>
</li>
</ul>
</div>
<div class="tab-content">
<div class="tab-pane" id="tab1">Tab1</div>
<div class="tab-pane" id="tab2">Tab2</div>
<div class="tab-pane" id="tab3">Tab3</div>
</div>
This is my Javascript:
(function(){
var hash = window.location.hash;
if(hash){
$("#profile_nav_tabs " + "a[href=" + hash + "]").tab('show');
}
$('#profile_nav_tabs a').on('shown.bs.tab', function(event){
window.location.hash = event.target.hash;
alert("hello");
// some ajax call
})
})();
Now, If I refresh the page, the url being local.dev/users/profile/#tab1, the tab1 content shows up, but the shown.bs.tab event does not fire. That is, neither the alert shows nor the ajax call is made.
The event is triggered only when I click on the <a data-toggle="tab" href="#tab1"> physically.
I want the url to be updated on each tab change and I also want a callback once the tab is showed on page load.
Does the shown.bs.tab event not fire on programmatically showing the tab?
There are two things necessary to get your code to run:
Propertly quote or escape hash (due to the # character) when it is inserted into jQuery's attribute equals selector. See jQuery Selectors, Category: Attribute:
Attribute values in selector expressions must follow the rules for W3C
CSS selectors; in general, that means anything other than a
valid identifier should be surrounded by quotation marks.
double quotes inside single quotes: $('a[rel="nofollow self"]')
single quotes inside double quotes: $("a[rel='nofollow self']")
escaped single quotes inside single quotes: $('a[rel=\'nofollow self\']')
escaped double quotes inside double quotes: $("a[rel=\"nofollow self\"]")
The variation you choose is generally a matter of style or convenience.
Linked in the above quote, W3C has the following to say on "valid identifiers" in CSS:
In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance, the identifier "B&W?" may be written as "B\&W\?" or "B\26 W\3F".
The # character is code point U+0023 and outside the range of characters allowed in a valid identifier.
Add the shown.bs.tab event handler before executing $.tab('show')
So change your script to the following:
(function(){
$('#profile_nav_tabs a').on('shown.bs.tab', function(event){
window.location.hash = event.target.hash;
alert("hello");
// some ajax call
});
var hash = window.location.hash;
if(hash){
$("#profile_nav_tabs " + 'a[href="' + hash + '"]').tab('show');
}
})();
This has to be placed after your HTML snippet in the DOM, but I assume that's already the case.
Also note that you were missing a semicolon after your event handler binding.
I have an onClick call on a link:
<a onClick="fomateName('Andrew Dsouza')"> //this is working good
The problem is the variable inside fomateName would contain single quotes and my fomateName Takes a variable like
var a='Andrew D'souza'. Need to format a variable present with single quote Ex;
<a onClick="fomateName('a')"> which turns to
<a onClick="fomateName('Andrew D'souza')"> //this is not working ,because present of single quote
Any Idea how to pass text with proper quotes in a javascript.
with single quote not the name actually
Try:
<a onClick="fomateName('Andrew D\'souza')"> <!-- this will work -->
\ use backslashes to escape '
Lets say if you have function like this =>
function fomateName(txt){
alert(txt);
}
and invoking it from anchor =>
<a onClick="fomateName('Andrew D\'souza')"> <!-- this will alert "Andrew D'souza" -->
Escape the quote with backslashes.
<a onClick="fomateName('Andrew D\'souza')">
//this is not working ,because present of single quote
You can wrap it in double quotes like so:
<a onClick="fomateName("Andrew D'souza")"> //this is not working ,because present of single quote
Never mind, just realized it already has double quotes, yeah use backslash for escape like so:
<a onClick="fomateName('Andrew D\'souza')">
Try this. Use backslash - It will escape the quote breaks
<a onClick="fomateName('Andrew D\'souza')">
You can use escape character
<a onclick="formateName('AdrewD\'souza')">
You can escape the quote with a backslash..
fomateName('Andrew D\'souza');
This should work anyway:
var name = "Andrew D'souza";
fomateName(name);