Is there a way to fix quotes that are inside of each other without them clashing? [duplicate] - html

This question already has answers here:
How to escape double quotes in a title attribute
(7 answers)
How do I properly escape quotes inside HTML attributes?
(6 answers)
Closed 2 days ago.
I'm making a list of links that have bookmarklets inside. The problem is that there are quotes in the bookmarklet that clash with the quotes. Is there a way to fix this, or otherwise is there a different way to do it?
Code:
<a href='javascript:(function() { var l = document.querySelector("link[rel*='icon']") || document.createElement('link'); l.type = 'image/x-icon'; l.rel = 'shortcut icon'; l.href = 'https://google.com/favicon.ico'; document.getElementsByTagName('head')[0].appendChild(l); document.title = 'Google';})();'>Code</a>
I tried changing the quote type, but that doesn't work. I want the javascript to be inside the link.

Related

Replacing innerHTML closes tag unnecessarily in Angular [duplicate]

This question already has answers here:
paragraph tag not closed?
(2 answers)
Closed 3 years ago.
I have a function in Angular that takes DOM content and does search and replace to annotate specific text. The problem is that the replaced text (using innerHTML) closes tags prematurely. Simplistically, it is reading:
}--><p _ngcontent-atr-c1="" class="paragraph-body ng-star-inserted"><div>Blah blah</div></p><!--bindings={
and thinks the <p> is not closed and the </p> is not opened, so the innerHTML is inappropriately closing and opening tags automatically like so:
}--><p _ngcontent-atr-c1="" class="paragraph-body ng-star-inserted"></p><div>Blah blah</div><p></p><!--bindings={
How do I resolve this?
My function (which looks for case variants of searchTerm to replace):
startSearch(searchTerm: string) {
const content = document.getElementById('chapter').children;
const regexLower = new RegExp(`${searchTerm.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')}`, 'g');
const regexUpper = new RegExp(`${searchTerm.toUpperCase().replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')}`, 'g');
const regexCapitalized = new RegExp(
`${searchTerm.replace(/^\w/,
c => c.toUpperCase()).replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')}`, 'g'
);
for (let i = 0; i < content.length; i++) {
const block = content[i].innerHTML;
block.replace(regexLower, `<span class="highlight">${searchTerm.toLowerCase()}</span>`);
block.replace(regexUpper, `<span class="highlight">${searchTerm.toUpperCase()}</span>`);
block.replace(regexCapitalized, `<span class="highlight">${searchTerm.replace(/^\w/, c => c.toUpperCase())}</span>`);
content[i].innerHTML = block;
}
}
You have malformed HTML according to the web browser.
It's disallowing <div> tags inside the <p> tag content.

html5 - can't format `\n` as new line in rendered string [duplicate]

This question already has answers here:
Why does the browser renders a newline as space?
(6 answers)
Closed 3 years ago.
I have the following tag but '\n' inside item.value not formatted correctly .
<td ng-if="flag">{{item.value}}</td>
HTML needs a <br/> tag. Use this regex on your value.
item.value = item.value.replace(/(?:\r\n|\r|\n)/g, '<br>');
let item = {};
item.value= "Hi I am some text with a \n line break";
item.value = item.value.replace(/(?:\r\n|\r|\n)/g, '<br>');
document.write(item.value);

RegEx for capturing an attribute value in a HTML element [duplicate]

This question already has answers here:
Extract Title from html link
(2 answers)
Closed 3 years ago.
I have a problem to extract text in the html tag using regex.
I want to extract the text from the following html code.
Google
The result:
TEXTDATA
I want to extract only the text TEXTDATA
I have tried but I have not succeeded.
Here we want to swipe the string up to a left boundary, then collect our desired data, then continue swiping to the end of string, if we like:
<.+title="(.+?)"(.*)
const regex = /<.+title="(.+?)"(.*)/gm;
const str = `Google`;
const subst = `$1`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
RegEx
If this expression wasn't desired, it can be modified or changed in regex101.com.
RegEx Circuit
jex.im also helps to visualize the expressions.
PHP
$re = '/<.+title="(.+?)"(.*)/m';
$str = 'Google';
$subst = '$1';
$result = preg_replace($re, $subst, $str);
echo $result;
Use this regex:
title=\"([^\"]*)\"
See:
Regex
Google
Remvoe Title and try

Regex to strip line comments from html [duplicate]

This question already has answers here:
RegEx for match/replacing JavaScript comments (both multiline and inline)
(17 answers)
Closed 5 years ago.
I am trying to remove unnecessary Line Comments from html & css. I've Found a regex to remove comments like these:
/* Write your comments here */
but what Im looking for is a regex to Multi Line Comments like these:
<!-- Write your comments here
Second line
third Line
-->
Currently Using this code to remove the single line comments:
<!--[^\[].*-->
Any assistance would be greatly appreciated
Better to generate a temporary DOM element and iterating over all the nodes recursively remove the comment nodes.
var str = `
<div>
test
<!-- test comment -->
<!-- test comment -->
test
<!--
test comment
multiline
-->
</div>`;
// generate div element
var temp = document.createElement('div');
// set HTML content
temp.innerHTML = str;
function removeEle(e) {
// convert child nodes into array
Array.from(e.childNodes)
// iterate over nodes
.forEach(function(ele) {
// if node type is comment then remove it
if (ele.nodeType === 8) e.removeChild(ele);
// if node is an element then call it recursively
else if (ele.nodeType === 1) removeEle(ele);
})
}
removeEle(temp);
console.log(temp.innerHTML);

How to write regular expression for found href of a tags? [duplicate]

This question already has answers here:
How to get one "a href" out of many in one html class with jSoup
(2 answers)
Closed 7 years ago.
I need to found href of a tags in string such as this .
<li>باغ بلور<span class="ur">bipardeh94.blogfa.com</span><span class="ds">فرهنگی-خبری-علمی</span></li>
<li>هزار نکته <span class="ur">avaejam.blogfa.com</span><span class="ds"> يك نكته از هزار نكته باشد تا بعد </span></li>
<li>روابط عمومی دانشگاه آزاداسلامی کنگاور<span class="ur">prkangavar.blogfa.com</span><span class="ds">اخبار دانشگاه</span></li>
I use this code :
string regex = "href=\"(.*)\"";
Match match = Regex.Match(codeHtml, regex);
if (match.Success)
{
textBox1.Text += match.Value +"\n";
}
This code found first href and then return all codes.
Does this regex work?
string regex = "href=\"([^\"]*)\"";
[^\"]* allows everything inside the href's quotes to be anything but a quote
For how to match all tags, please use Regex.Matches