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

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);

Related

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

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.

BeatifulSoup Extract String in div tag [duplicate]

This question already has answers here:
how to get text from within a tag, but ignore other child tags
(2 answers)
Closed 2 years ago.
I have the following HTML:
<div class="interesting"><span>a</span> <span>b</span> c</div><div>d</div>
I am trying to use beautifulsoup to extract the string c.
However, soup.div.string is None. I could call get_text() to get a b c and then I parse the text again. But I feel it defeats the purpose of using beautifulsoup.
Any suggestion?
=====================
Update:
I added to my example string above as I noticed that it actually causes soup.div.find(text=True, recursive=False) fails to return text in div. So this question isn't a duplicate anymore.
soup = BeautifulSoup('<div class="interesting"><span>a</span> <span>b</span> c</div><div>d</div>', 'html.parser')
div = soup.find('div', class_='interesting')
print(div.find_all_next(text=True)[-1])
above code prints d
This should help you:
div = soup.find('div',class_ = "interesting")
print(div.find_all(text=True)[-1].strip()) #Prints the last text present within the div tag
Output:
c
Here is the full code:
from bs4 import BeautifulSoup
html = '<div class="interesting"><span>a</span> <span>b</span> c</div><div>d</div>'
soup = BeautifulSoup(html,'html5lib')
div = soup.find('div',class_ = "interesting")
print(div.find_all(text=True)[-1].strip())

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.

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

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