Regex in combination with jQuery text() method - javascript

$(document).ready(function(){
var regex1 = $('#element').text().match(/\b(06\d{8})\b/g);
console.log('regex1: ' + regex1);
var regex2 = $('#element').text().match(/(06\d{8})/g);
console.log('regex2: ' + regex2);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id = "element">
0600000000
0600000001
<div>0600000002</div> 0600000003
TEST:0600000004
<span>0600000005</span><span>TEST0600000006</span><span>0600</span><span>000007</span>
</div>
What is want is only 6 matches 0600000000-0600000005.
Looks great when I use this regex: /\b(06\d{8})\b/g https://regex101.com/r/fE6gJ7/1
But.., when I use the jQuery text() it looks like the output of text() replaces every HTML tag with "nothing":
Output $(element).text()
0600000000
0600000001
0600000002 0600000003
TEST:0600000004
0600000005TEST06000000060600000007
Is it possible to avoid this? Or can I replace every HTML tag with a single space?
Or what is the alternative(iterate over all text elements?)

Use .html() insetad of .text(). .text() will remove the HTML tags inside the element and the textContent will be concatenated with other text.
So, using text() on the parent element for following
<span>0600000005</span><span>TEST0600000006</span><span>0600</span><span>000007</span>
will give
0600000005TEST06000000060600000007
And regex will not capture anything here because of word boundary.
Demo:
$(document).ready(function(){
var regex1 = $('#element').html().match(/\b(06\d{8})\b/g);
console.log('regex1: ' + regex1);
var regex2 = $('#element').html().match(/(06\d{8})/g);
console.log('regex2: ' + regex2);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id = "element">
0600000000
0600000001
<div>0600000002</div> 0600000003
TEST:0600000004
<span>0600000005</span><span>TEST0600000006</span><span>0600</span><span>000007</span>
</div>

Related

Remove HTML tags and formatting text

I would like to remove HTML tags between text and change newline to space. I'm using this pattern below but it is not perfectly. It adds two or more space between text. How to fix this pattern?
replace(/( |<([^>]+)>)/ig, ' ');
try below code and check
replace(/(<([^>]+)>)/ig,"");
UPDATE
You can do this way,
var html = 'Example: <h1></h1><p></p><div> </div><div>CONTENT</div> ';
html = html.replace(/\s|\n| /g, ' ');
html = html.replace(/<[^>]+>/gm, '');
Output will be like this,
Example: CONTENT
Play around the above solution & you will succeed.
Here is how I'll do what you want:
(See comments in my snippet)
// Input data
var input_data = `My<div><br>
<span></span>
<span></span>
</div><p>Content</p>`;
console.log("Input:", input_data);
// Creates html element with Input data
var elm = document.createElement('div');
elm.innerHTML = input_data;
// Use native function '.innerText' to get rid of the html,
// then replace new lines by spaces, and multiple spaces by only one space
output_data = elm.innerText.replace(/\n/g, ' ').replace(/[\s]+/g, ' ');
console.log("Output:", output_data);
Hope it helps!

remove html tags from string js/jquery

I'm trying to show a text that i'm getting from the server with html tags.
Let's say i got
"a\nb\nc\nd\ne\nf\n"
and i want to show
a
b
c
d
e
f
i tried using jquery text() but i get empty string:
var answer = params.question.answer;
$('#answer_text').html($(answer).text());
i also tried with regex but nothing happens:
var regex = /(<([^>]+)>)/ig;
var answer = params.question.answer.replace(regex, '');
$('#answer_text').html(answer);
You need to convert \n into <br/> for creating line breaks in html:
var answer= "a\nb\nc\nd\ne\nf\n";
$('#answer_text').html(answer.replace(/\n/g, "<br />"));
Working Demo
Using REGEX Remove \n and add <br /> tag.
Try:
var answer = "a\nb\nc\nd\ne\nf\n";
var regex = /\n/gi;
$('#answer_text').html(answer.replace(regex, "<br />"));
Demo
Another option is to use the white-space rule
var answer = "a\nb\nc\nd\ne\nf\n";
$('#answer_text').html(answer);
#answer_text {
white-space: pre-line;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="answer_text"></div>

How to get Html Element's Text

I want to extract the text part of the html.
If I have <p>ABCD</p>
I want the out put to be ABCD
Something like,
var html='<p>ABCD</p>';
var str = convertToString(html);
Hope, I will need a function which converts from html to string, or maybe extract string from it.
You can use jQuery to extract the text content from the string
var html = '<p>ABCD</p>';
var str = $(html).text();//get a jQuery reference and then read its text content
console.log(str)
All you need is a selector(id,class name etc) of get the required element of DOM and use .text() to get text part of the html.
HTML
<p>ABCD</p>
Jquery
var str = $('p').text(); // $('p') "p" is a selector to select p element(s)
console.log(str)
DEMO
JavaScript way
var txt = document.getElementById("demo").innerHTML;
alert(txt);
<p id="demo">Test text</p>
var html = '<p>ABCD</p>';
var str = $(html).text();
console.log(str);
This would do the task.

Prevent html tag creation during html() using Jquery

I am having a string "<F1>" and whenever I add it as html to a particular div using jquery's html(), as
var str = "<F1>";
$("div").html(str);
It generates html for div as
"<f1></f1>"
But I dont want such tag creation.
I need to have div with html as
"<F1>"
It will be appreciated if somebody guide me, to achieve this. :(
Use .text() instead of .html().
var str = "<F1>";
$("div").text(str);
Fiddle: http://jsfiddle.net/6942a/
To escape the HTML entities in str and use .html() you can do the following:
var str = "<F1>";
str = $("<div/>").text(str).html();
$("div").html(str);
Updated jsfiddle: http://jsfiddle.net/6942a/3/
Use this :
HTML:
<div></div>
<input type="text">
<input type="button" value="ADD">
jQuery :
$(function(){
$('input[type=button]').click(function(){
var text = $('input[type=text]').val();
text =text.replace('<','&#60');
text =text.replace('>','&#62');
$("div").html(text);
});
});
Demo
For more information see html entities
Try this
var str = '<f1>';
var res = str.replace("<", "<");
res=res.replace(">", ">");
$("div").text(res);
Fiddle

Adding bold tags to a string of html

I have a string of html in javascript/jquery.
var str = '<div id="cheese" class="appleSauce"> I like apple and cheese</div>';
I want to make the string 'apple' bold. So I do:
str = str.replace('apple','<b>apple</b>');
but this breaks the html part of the string. I get:
<div id="cheese" class="<b>apple</b>Sauce"> I like <b>apple</b> and cheese</div>
How can I replace all occurrences of a string in the text of an html string without changing the matches inside of html markup?
var e = $('#cheese');
e.html(e.text().replace('apple','<b>apple</b>'));
Working Fiddle
Create an element, jQuery element in this case, and set the innerHTML property:
var el = $('<div id="cheese" class="appleSauce"> I like apple and cheese</div>');
el.html(el.html().replace('apple','<b>apple</b>'));
You can do it like that
var str=str.replace(new RegExp(/(apple)$/),"<b>apple</b>");

Categories

Resources