I'm trying to replace a word in a textarea with another text, but I cannot seem to get newlines to work.
<input type="text" id="testing" value="Newline \n test" /><br />
<textarea>test</textarea><br />
<button>Test</button>
$("button").on("click", function() {
$("textarea").text($("textarea").text().replace(/test/g, $("#testing").val()));
});
Press the button. It will not replace the \n with a new line. I tried <br />, <br> (incorrect HTML), %0A and
but it still does not work.
Fiddle
You need to replace the character string '\n' (2 characters '\','n') with the actual \n line break character.
This should do what you need.
var textBoxline = $("#testing").val().replace(/\\n/g, '\n');
$("textarea").text($("textarea").text().replace(/test/g, textBoxline));
You will still need to make allowances for leading/trailing spaces around the \n itself.
Related
I'm trying to replace new line characters (\n) with html tag's. But this should not happen if the line ends with an another html tag.
Like this:
Line 1<br />
Line 2<br />
<p>Hello World</p>\n
Line 4<br />
So my question now is why isn't the following regex working? Now are on every line tags not just on the lines without the <\p> tag.
/(?!<.*>)\n/g
That regex is working if I dont want to have a <\br> tag if the next line doesnt contain html tags.
/\n(?!.*<.*>)/g
You need some logic with the following expression here:
.+<[^\n\r]+>$|(.+)
In JavaScript:
var html = `Line 1
Line 2
<p>Hello World</p>
Line 4`;
var converted = html.replace(/.+<[^\n\r>]+>$|(.+)/gm, function(match, group1) {
if (typeof(group1) == "undefined") return match;
else return group1 + "<br>";
});
console.log(converted);
The idea is to match lines ending with a potential tag but to capture those without, see a demo on regex101.com.
While copying text from word file to text editor I am getting html code like,
<p><br></p>
<p> <br></p>
<p> <br></p>
<p> <br></p>
I want to replace above code with empty text like this,
var updated = copyieddata.replace('<p><br></p>', '');
updated = updated.replace('<p> <br></p>', '');
updated = updated.replace('<p> <br></p>', '');
updated = updated.replace('<p> <br></p>', '');
How to implement above functionality by using Regex to avoid repetition.
pedram's answer is probably the easiest way to achieve what you want.
However, if you want to only remove the <p> <br></p> tags and keep all other tags intact, then you need a regular expression that gets all parts of your string that:
Start with <p> and end with </p>
Have only <br> or whitespace in between
The regular expression you need would look like this: /<p>(\s|<br>)*<\/p>/g
This expression looks for any substring that starts with <p>, has zero or more occurrences of either whitespace (\s) or the <br> tag, and ends with </p>.
The /g at the end ensures that if there are multiple occurrences of the pattern in the string, then every pattern is matched. Omitting /g would match only the first occurence of the pattern in your string.
So, your code would look something like this:
var pattern = /<p>(\s|<br>)*<\/p>/g;
var updated = copyieddata.replace(pattern, '');
The simplest way is convert html to text (it remove all additional html tags, and you get clean text) but also you use this topics to learn how format ms word texts.
Jquery Remove MS word format from text area
Clean Microsoft Word Pasted Text using JavaScript
var text = $('#stack');
text.html(text.text());
console.log(text.html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="stack">
some text
<p><br></p>
<p> <br></p>
<p> <br></p>
<p> <br></p>
some text
</div>
Or you use this to replace all <br> and <p> tags.
$("#stack").html(
$("#stack").html()
.replace(/\<br\>/g, "\n")
.replace(/\<br \/\>/g, "\n")
.replace(/\<p>/g, "\n")
.replace(/\<\/p>/g, "\n")
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="stack">
some text
<p><br></p>
<p> <br></p>
<p> <br></p>
<p> <br></p>
some text
</div>
Instead of "\n" you can use nothing like this ""
I have this code
<script>
var str = "first line <br /> second line <br /> third line";
$(".div_class").prepend(str);
</script>
This code do not add break like below
first line
second line
third line
Rather it shows like first line <br /> second line <br /> third line
but I want like the first one so that br tags work. How can I do this?
I am working on ruby on rails
You are outputting to text in an HTML context (such as nodeValue or innerText, you will have to use \n and ensure that the container has the CSS white-space property set to something that doesn't condence whitespace, such as pre-wrap.
Use \r\n instead of br tags. I'd post the code for you but I'm on my phone and I can't use the shortcut keys to do so.
Consider the following HTML page fragment:
<div id='myDiv'>
Line 1.<br />
Line 2<br />
These are <special> characters & must be escaped !##><>
</div>
<input type='button' value='click' id='myButton' />
<textarea id='myTextArea'></textarea>
<script>
$(document).ready(function () {
$('#myButton').click(function () {
var text = $('#myDiv').text();
$('#myTextArea').val(text);
});
});
</script>
First, there is a div element with id myDiv. It contains some text similar to what might be retrieved form a SQL database at runtime in my production web site.
Next, there is a button and a textarea. I want the text in myDiv to appear in the textarea when the button is clicked.
However, using the code I provided, the line-breaks are stripped out. What can I do about this, taking into consideration that escaping special characters is absolutely non-negotiable?
Your code works great for me in both Firefox and Chrome: http://jsfiddle.net/jYjRc/
However, if you have a client that doesn't do what you want, replace <br>s with newline characters.
Edit: Tested in IE7 and the code breaks. So I updated the fiddle with my suggestion: http://jsfiddle.net/jYjRc/1/
Do your HTML like so:
<div id='myDiv'><pre>
Line 1.
Line 2
These are <special> characters & must be escaped !##><>
</pre></div>
And now .text() will return the text exactly as you specify it in the <pre> tag, even in IE.
I'm trying to replace any <br /> tags that appear AFTER a </h2> tag. This is what I have so far:
Text = Text.replace(new RegExp("</h2>(\<br \/\>.+)(.+?)", "g"), '</h2>$2');
It doesn't seem to work, can anyone help? (No matches are being found).
Test case:
<h2>Testing</h2><br /><br /><br />Text
To:
<h2>Testing</h2>Text
This is simpler than you're thinking it out to be:
Text = Text.replace(new RegExp("</h2>(\<br \/\>)*", "g"), "</h2>");
This would do what you are asking:
Text = Text.replace(new RegExp("</h2>(<br />)*", "g"), '</h2>');
If you have jQuery kicking around then you can do this safely without regular expressions:
var $dirty = $('<div>').append('<p>Where is<br>pancakes</p><h2>house?</h2><br><br>');
$dirty.find('h2 ~ br').remove();
var clean = $dirty.html();
// clean is now "<p>Where is<br>pancakes</p><h2>house?</h2>"
This will also insulate against the differences between <br>, <br/>, <br />, <BR>, etc.
You can also make this a little nicer? using the shorthand regex syntax
Text = Text.replace(/<\/h2>(<br\s*\/>)*/g, '</h2>');