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).
Related
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});
How to show the following Javascript code, but not run it?
<script>alert("test");</script>
I tried to show this code on my page, but when user loads the page it alerts "test".
How can I show the code but not run it on page load?
Use < for < and > for >
<code>
<script>alert("test");</script>
</code>
<pre><code><script>alert('test')</script>
</code></pre>
OR
Using Jquery you can simply add text on particular selector using .text()
Jquery example:
$(document).ready(function () {
$('.showText').text("<script>alert('text')<"+"/script>");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<span class="showText"></span>
<!-- Sadly (totally)... this is obsolete :( -->
<xmp>
<script>alert("test");</script>
</xmp>
http://www.w3.org/TR/REC-html32#xmp
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/plaintext
https://code.google.com/p/doctype-mirror/wiki/PlaintextElement
http://www.blooberry.com/indexdot/html/tagpages/x/xmp.htm
http://www.blooberry.com/indexdot/html/tagpages/p/plaintext.htm
;) Great stuff but sadly you're on the browser's mercy since <plaintext> and <xmp> are obsolete,
so
escaping the < and > characters with < and > (or with hex <, >)
is a far better idea.
<script>alert("test");</script>
You can wrap the above for semantic reasons in tags like <pre> or <code> although it's not needed to achieve the desired.
So the point is that we're over and over suggested to use <pre>, yet we still miss a proper and valid tag to literalize and represent <, &, > without escaping.
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>]*)[^>]*>
I'm having a problem writing a regular expression for matching HTML tags. I found a similar entry here, but this didn't quite work in my case.
Here's my test string:
<div id="div0" class="myclass">here's some text
that may include whitespace</div><div id="div1" class="myclass">
and some more here
</div>
And here's my regex based on the aforementioned entry:
<div[^>]*class="myclass">[^~]*?<\/div>
Note that I need to match the first instance of <div /> with class of "myclass." The content may have carriage returns. These <div> tags won't be nested.
Here's a rubular page for testing: http://rubular.com/r/vlfcikKMXk
That regex tested is not great. It is in fact matching as you want it to, but it is matching it multiple times (2 different matches), and not showing a difference, you only want the first match.
Go here:
http://gskinner.com/RegExr/
Test it there, turn off the 'global' you will see it working.
I have a string in JavaScript and it includes an a tag with an href. I want to remove all links and the text. I know how to just remove the link and leave the inner text but I want to remove the link completely.
For example:
var s = "check this out <a href='http://www.google.com'>Click me</a>. cool, huh?";
I would like to use a regex so I'm left with:
s = "check this out. cool, huh?";
This will strip out everything between <a and /a>:
mystr = "check this out <a href='http://www.google.com'>Click me</a>. cool, huh?";
alert(mystr.replace(/<a\b[^>]*>(.*?)<\/a>/i,""));
It's not really foolproof, but maybe it'll do the trick for your purpose...
Just to clarify, in order to strip link tags and leave everything between them untouched, it is a two step process - remove the opening tag, then remove the closing tag.
txt.replace(/<a\b[^>]*>/i,"").replace(/<\/a>/i, "");
Working sample:
<script>
function stripLink(txt) {
return txt.replace(/<a\b[^>]*>/i,"").replace(/<\/a>/i, "");
}
</script>
<p id="strip">
<a href="#">
<em>Here's the text!</em>
</a>
</p>
<p>
<input value="Strip" type="button" onclick="alert(stripLink(document.getElementById('strip').innerHTML))">
</p>
Regexes are fundamentally bad at parsing HTML (see Can you provide some examples of why it is hard to parse XML and HTML with a regex? for why). What you need is an HTML parser. See Can you provide an example of parsing HTML with your favorite parser? for examples using a variety of parsers.
If you only want to remove <a> elements, the following should work well:
s.replace(/<a [^>]+>[^<]*<\/a>/, '');
This should work for the example you gave, but it won't work for nested tags, for example it wouldn't work with this HTML:
<em>Google</em>
Just commented about John Resig's HTML parser. Maybe it helps on your problem.
Examples above do not remove all occurrences. Here is my solution:
str.replace(/<a\b[^>]*>/gm, '').replace(/<\/a>/gm, '')