jQuery convert line breaks to br (nl2br equivalent)
demo: http://so.devilmaycode.it/jquery-convert-line-breaks-to-br-nl2br-equivalent
function nl2br (str, is_xhtml) {
var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}
- http://phpjs.org/functions/nl2br:480
nl2br() equivalent in javascript
Take a look at nl2br on php.js which seems exactly what you're looking for. Basically, it's:
function nl2br (str, is_xhtml) {
if (typeof str === 'undefined' || str === null) {
return '';
}
var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
}
EDIT:your example using
nl2br()
may be changed like this:$('#TextArea').keypress(function(evt){
$('#TextArea1').html(nl2br($('#TextArea').val()));
});
(note that this updates #TextArea1
on every keypress and doesn't change the value of #TextArea
wich is what I think you're looking for, but I might be misunderstanding)EDIT2:
If you want to get the behaviour of your old function (with inserting <br/>
s to #TextArea
) do this:
$('#TextArea').keypress(function(evt){
$('#TextArea').html(nl2br($('#TextArea').val())); // replace linebreaks first
$('#TextArea1').html($('#TextArea').val()); // copy to #TextArea1
});
How do I replace all line breaks in a string with br / elements?
This will turn all returns into HTML
str = str.replace(/(?:\r\n|\r|\n)/g, '<br>');
In case you wonder what ?: means.It is called a non-capturing group. It means that group of regex within the parentheses won't be saved in memory to be referenced later.
You can check out these threads for more information:
https://stackoverflow.com/a/11530881/5042169
https://stackoverflow.com/a/36524555/5042169
How to replace \n with br / in JavaScript?
Replace with global scope
$('#input').val().replace(/\n/g, "<br />")
or$('#input').val().replace("\n", "<br />", "g")
How can I replace newline characters using javascript in IE8?
When you retrieve element's innerHTML, IE will convert the innerHTML to a "standard-format" (by collapsing multi-spaces into one, removing linebreak, etc...) before giving you the result.
Thus, you can not find any linebreak character in the innerHTML you get with IE. What a bad news.
I think the most feasible & easy approach is to store your text inside <textarea>
tag instead of normal <div>
. IE will leave <textarea>
alone when you get it's value instead of innerHTML:
originalText=document.getElementById('EULA_content').value
Of course, when you get the newText, you should append it to another div element. Convert \n line breaks to HTML BR line breaks
You are trying to use a string method on an object, not on property values within that object.
There is no Object.replace()
so that will throw error
Similarly undefined.replace()
will also cause error
Presumably you only have to fix a few property values but first it is necessary to make sure those values are defined and are strings before using replace()
Try
function convertBreak(str){
if(!str){
return ''; // don't want `undefined` printing into page
}
// if it's something other than string, return it as is
if( typeof str !=== 'string'){
return str;
}else{
return str.replace(/\n/g, "<br />")
}
}
incident.Description = convertBreak(incident.Description);
Convert \n line breaks to HTML BR line breaks
You are trying to use a string method on an object, not on property values within that object.
There is no Object.replace()
so that will throw error
Similarly undefined.replace()
will also cause error
Presumably you only have to fix a few property values but first it is necessary to make sure those values are defined and are strings before using replace()
Try
function convertBreak(str){
if(!str){
return ''; // don't want `undefined` printing into page
}
// if it's something other than string, return it as is
if( typeof str !=== 'string'){
return str;
}else{
return str.replace(/\n/g, "<br />")
}
}
incident.Description = convertBreak(incident.Description);
Related Topics
How Variables Are Allocated Memory in JavaScript
Fix the Upstream Dependency Conflict Installing Npm Packages
Use of @ Symbol in Node Module Names
Scroll Smoothly to Specific Element on Page
Properties of JavaScript Function Objects
Why Is 'This' Undefined Inside Class Method When Using Promises
How to Randomly Generate HTML Hex Color Codes Using JavaScript
Mousewheel Event in Modern Browsers
What Causes the Error "Can't Execute Code from a Freed Script"
Why Doesn't Nodelist Have Foreach
Why Does This Foreach Return Undefined When Using a Return Statement
How to Create a Stopwatch Using JavaScript
Printing a Web Page Using Just Url and Without Opening New Window
Why Do I Need to Await an Async Function When It Is Not Supposedly Returning a Promise
Mocking a Useragent in JavaScript
Detect Whether Scroll Event Was Created by User