I'm current making a simple application using NodeJS to translate input into a defined format. For this I'm using the following piece of JavaScript, where content is the input.
content = content.replace(/(.+)\n=+$/gm, '<div>$1</div>');
content = content.replace(/(.+)\n-+$/gm, '<p>$1</p>');
Using this, I would expect the code below
Message
======
Another Message
------
translate into
<div>Message</div>
<p>Another Message</p>
However, I get the same output as input (so nothing changed),
I tried it with both RegExr and WebStorm's RegEx tester, and both of those find a match. When I log the result of content.match(/(.+)\n=+$/gm) I get null.
When I remove the \n from the RegEx and the input, it does seem to match, which has me think the \n is causing some kind of issue. However, I'm not aware of any issue this could be causing.
are you using windows?
give a try to:
content = content.replace(/(.+)\r\n=+$/gm, '<div>$1</div>');
content = content.replace(/(.+)\r\n-+$/gm, '<p>$1</p>');
depending on OS and browser you may get there \n or \r\n (\r denotes carriage return, they may be other reasons for them to appear. But in your regex you should expect either \n or \r\n
Edit:
As suggested by Poul Bak you could simply add ? after \r to handle both cases:
content = content.replace(/(.+)\r?\n=+$/gm, '<div>$1</div>');
content = content.replace(/(.+)\r?\n-+$/gm, '<p>$1</p>');
Related
I'm using regular textbox as a text input where the users wrties their comments. I then use JQuery and JSON to send data to the server and then insert it into the database.
When I want to display this text I use jQuery to download it prepare HTML and display it in the browser, but there are no new lines.
How can I keep any newlines entered by the user so that they are displayed in the browser?
EDIT:
The problem is that when I do alert $('.detailsCommentContent').val() I can see line breaks in the alert window, but when I then pass it as a GET argument:
insertComment.aspx?id=10&content= " + $('.detailsCommentContent').val() "
then in the url there are no signs of newLine :(
Just do like this answer: keep formatting entered in asp.net textbox (carriage return, new line, etc)
theStringYouWantToFormat.Replace(char.ConvertFromUtf32(13),"<br/>")
Before writing out the HTML using javascript to the page, make sure to replace all the newlines with <br /> tags. Here is a simple extension for string that will allow you to do it using javascript (source):
String.prototype.NewlineToBR = function() {
return this.replace( /\r\n|\r|\n/g, br || '');
}
Usage:
var htmlString = newlineString.NewlineToBR();
Then just insert the new string into you HTML.
where do you want to display the text? in a textarea or directly on the page?
if on the page you'll have to convert the newlines to <br/> tags when getting the text from the db and printing it to the page.
I beleive this is down to the encoding you are using. Difference between unicode and ascii or something similar. It's been a while since I worked on something like this but I think it boiled down to two options.
match up the encoding on save and on load (we found that we had ascii on one and unicode on another).
replace all new line character with an arbituary value when saving and swap it back when you load it.
I wanted to understand what carriage return is by writint a simple code to console.As carriage return '\r' means
" return to the beginning of the current line without advancing
downward"
But in my code the following string is appended at the end of the line .Why it is behaving like this.I have a string "this is my string" ,then i have carriage return ,and it is followed by another string "that".I thought "that" will be placed at the beginning of the string
console.log("this is my string"+String.fromCharCode(13)+"that");
it prints "this is my stringthat"
Using \r in a string in JavaScript is probably going to give you different results depending on a combination of how the program is being run (in a browser or a standalone engine) and the target of the text (console, alert, a text node in an HTML element etc). It's not clear from your question whether you're running JavaScript in a browser, but (assuming you are) you're going to get different results for different browsers. Internet Explorer's console treats \r as a newline character (\n) while most other browsers will ignore it. I doubt any browser implementation of console is going to give you the behavior you've described.
Note that \r is not a string processing instruction, it's a character. Doing this:
var aString="one\r2";
is never going to result in
aString == "2ne"
or
aString == "2one"
or
aString == "one2"
or anything similar evaluating to true. aString's value will remain "one\r2" until you change it. It's up to the console or alert that is displaying the string to choose how to render \r.
There are string processing methods in JavaScript for splitting and recombining strings (see the w3schools Javascript String Reference or Mozilla's String reference) that would better suit your purposes. If you start using characters like \r or \b in other languages and/or environments you're going to encounter different behaviors based on a whole host of factors.
I'm writing a Photoshop Javascript script file. For all intents and purposes, this script when ran replicates a specific text layer several times. If the original text layer contains an apostrophe, the replicated instances replace the apostrophe with a square block. So "It's" becomes "It[]s" (obviously not brackets, but the square block.)
Here is the code:
titleLayer = al.textItem.contents;
newTitleLayer = titleLayer.replace("'", "\'");
alert(newTitleLayer); // At this point, this works: "It's"
persistentSetting.putData(0,newTitleLayer);
app.putCustomOptions("text_contents4",persistentSetting,true);
alert(persistentSetting.getData(0)); // At this point, it does not. It shows the square. "It[]s"
I know this has to be a simple issue, I've just never encountered this before.
Thanks.
I guess you want
newTitleLayer = titleLayer.replace(/'/g, "\\'");
// ^^^^ ^
// regex to match *all* apostrohpes escape the backslash
What ended up working for me is:
persistentSetting.getData(0).replace("EM", "'"); // It's not actually EM, but that's the little code that JS shows in my editor when I copied and pasted that special block [] character in.
Thanks for the help.
This is probably a simple one but I'm very much a regex novice.
I'm looking to select the first line of every paragraph within a textarea on a page using a regular expression. After thinking I was there I have hit a problem.
Using http://gskinner.com/RegExr/ I came up with this:
/\r\r.*\r/g
but then I place that into my javascript and ran it on the page:
var headingsArr = document.getElementById("text").value.match(/\r\r.*\r/g);
and the array returns null.
Have I got the regular expression right and if so where am I going wrong when using it in my javascript!?
Thank you
This depends on what your newline characters are. I think you may better go for
/(?:\r\n|[\r\n]){2}.*(?:\r\n|[\r\n])/g
I know in Regexr only a \r is a newline. But in Windows normally \r\n is used, but under .*ix its normally only the \n.
So (?:\r\n|[\r\n]) is an alternation, it tries at first to match \r\n if this is not found it matches either \r or \n.
For the sake of future searchers, if you just need to style the first line of text, you can use the css pseudoclass ::first-line :
textarea::first-line {
background-color: yellow;
}
http://www.w3schools.com/cssref/sel_firstline.asp
I'm using regular textbox as a text input where the users wrties their comments. I then use JQuery and JSON to send data to the server and then insert it into the database.
When I want to display this text I use jQuery to download it prepare HTML and display it in the browser, but there are no new lines.
How can I keep any newlines entered by the user so that they are displayed in the browser?
EDIT:
The problem is that when I do alert $('.detailsCommentContent').val() I can see line breaks in the alert window, but when I then pass it as a GET argument:
insertComment.aspx?id=10&content= " + $('.detailsCommentContent').val() "
then in the url there are no signs of newLine :(
Just do like this answer: keep formatting entered in asp.net textbox (carriage return, new line, etc)
theStringYouWantToFormat.Replace(char.ConvertFromUtf32(13),"<br/>")
Before writing out the HTML using javascript to the page, make sure to replace all the newlines with <br /> tags. Here is a simple extension for string that will allow you to do it using javascript (source):
String.prototype.NewlineToBR = function() {
return this.replace( /\r\n|\r|\n/g, br || '');
}
Usage:
var htmlString = newlineString.NewlineToBR();
Then just insert the new string into you HTML.
where do you want to display the text? in a textarea or directly on the page?
if on the page you'll have to convert the newlines to <br/> tags when getting the text from the db and printing it to the page.
I beleive this is down to the encoding you are using. Difference between unicode and ascii or something similar. It's been a while since I worked on something like this but I think it boiled down to two options.
match up the encoding on save and on load (we found that we had ascii on one and unicode on another).
replace all new line character with an arbituary value when saving and swap it back when you load it.