Javascript Newline Character Throwing Error - javascript

I am having trouble getting some data from a textbox using javascript. What I am trying to do is take the value from a textbox and put it in an alert and copy it. Here is the code that I currently have:
var copyString = "Date: <%= TXT_Details_DateReq.Text %>;
window.prompt('Press CTRL+C, then ENTER\n\nNOTE: SAVE ANY CHANGES BEFORE COPYING TEXT!', copyString); return false;
So this code works perfectly fine if the text in the textbox is just one line. But if the text in the textbox has multiple lines such as:
"This is one line
here is a second line"
The code will throw the error Uncaught Syntax Error: Unexpected Token ILLEGAL.
From what I have researched this throws when there is an illegal character, so I believe it is the CRLF character from the textbox.
Any idea how to fix this?
Thanks.

New line characters might be preceded by return characters ('\r').
Swap '\n' with '\r\n' and you should be good to go.
Or even better: just handle all cases of the new line character instead of checking which case then applying it. This replace the newline:
htmlstring = stringContainingNewLines.replace(/(\r\n|\n|\r)/gm, "<br>");
Resource
hope it helps.

Related

How do I create line breaks in JavaScript?

I am using some code I found on this site and need to know how to insert a line break instead of a comma.
I have tried using \n as follows:
if(flg>0){
app.alert('There are '+flg+' fields that require a value\n\n'**+ fArr,3**)
}
But keep getting a syntax error. What am I doing wrong?
Adding the following text:
"\n"
on where you want the line break, definitely causes a line break. E.g. onclick="alert('Hi \n How are you?')"
You may be getting a syntax error because how you've entered asterisks. Try wrapping them with quotation marks too?
\n is what you require to get a line break and is correct. Your syntax error occurs because alert('There are '+flg+' fields that require a value\n\n'**+ fArr,3**) is not a valid method - unless you have overwritten it, alert() is a method that only takes one argument - a string.
'There are '+flg+' fields that require a value\n\n'**+ fArr,3** is not a valid string. Try instead 'There are '+flg+' fields that require a value\n\n**'+ fArr+',3**'.

How can I use a Mirth-Javascript to remove line breaks in HL7 messages?

An HL7 message comes into Mirth and throws a "processing" error. At the very bottom of the message in Raw format is a partial line that has been separated from the line above it. I have to manually correct this every time. I am hoping to use a Mirth-Javascript as a message filter that can fix this so that everything flows without human intervention.
Below message snippet triggers the error. In this example it is the very last line of the HL7 message.
OBX|68|FT|PT6663&IMP^PET/CT Imaging Whole Body||
||||||F|||202254836969552|||
Currently my only fix is to open the HL7 message and manually go to the line break and bring it up to the line above it that is part of the segment.
The HL7 message should look like this:
OBX|68|FT|PT1103&IMP^PET/CT Imaging Whole Body||||||||F|||20190327101958|||
This worked. Put the following into the preprocessor.
message = message.replace(/[\r\n]+(?![A-Z][A-Z][A-Z0-9]\|)/g, "");
return message;
Remove all line brakes in the channel's pre-processor or attachment script, and then insert them back based on the segment names.
The best way would be to stop the message generating system insert line brakes into OBX.5 field.
Removing all line breaks would be an approach, but it could be a problem later on, you could set up a replace script, that instead of '/n', searches for '|/n|' or a similar string, that way, it would fix that particular problem as well as any other undesired line breaks in between vertical separators, tho it wouldnt help if it broke anywhere else, so keep that in mind.
Put this code snippet in your preprocessor script. It worked for me
var newmessage = message.replace(/[\n\r]$/,"");
while (newmessage.match(/(\r\n|\r|\n)([^A-Z]|[A-Z][^A-Z]|[A-Z]{2}[^A-Z\d]|[A-Z]{2}[\d][^|]|[A-Z]{3}[^|])/i)) {
var extrabit = newmessage.match(/(\r\n|\r|\n)([^A-Z]|[A-Z][^A-Z]|[A-Z]{2}[^A-Z\d]|[A-Z]{2}[\d][^|]|[A-Z]{3}[^|])/i)[0].substring(1);
var newmessage = newmessage.replace(/(\r\n|\r|\n)([^A-Z]|[A-Z][^A-Z]|[A-Z]{2}[^A-Z\d]|[A-Z]{2}[\d][^|]|[A-Z]{3}[^|])/i,'\\.br\\' + extrabit);
}
return newmessage;
Mirth processor expects every line the first 3 characters should contain valid HL7 segments otherwise the mirth throws an error.
To remove the invalid line breaks in the HL7 message you should follow the below steps.
1.Channel -->Scripts -->Preprocessor.
Paste the bellow code top of the "return message;" statement
message = message.replace(/[\r\n]+(?![A-Z][A-Z][A-Z0-9]\|)/g, ""); //This Line is for invalid line breaks in incoming message APPEND to PREVIOUS SEGMENT .
Save the changes and deploy the channel for new changes affected.
From your question, the HL7 field that contains line breaks is OBX(5,1) which should hold Observation Value.
Observation value may contain line breaks as a part of data. Line break (<CR> or ASCII 13) is segment separator by default. If this is received as a part of data, there will be issues while parsing message. This is the root cause of the problem you mentioned in the question.
The segment separator is not negotiable. It is always a carriage return. I have explained this in more details in this answer.
Ideally, those line breaks should be replaced with its escape sequence while building HL7 message. More details about it are already given in one of my earlier answers here.
So, your inbound message
OBX|68|FT|PT6663&IMP^PET/CT Imaging Whole Body||
||||||F|||202254836969552|||
should be actually
OBX|68|FT|PT6663&IMP^PET/CT Imaging Whole Body||\X0D\\X0D\||||||F|||202254836969552|||
About your actual question that how to do this with Mirth/Javascript, there should not be need in your particular use case. This conversion should be done before sending message to Mirth. So, the one who is sending this message to you should build it like this.
While actually displaying observation value on UI, you again need to do the reverse process.
Edit:
If line break is different than <CR> (ASCII 13), then respective HEX should be replaced in \X0D\. Details are mentioned in my linked answer; I am not repeating those here.
I had the similar issue of having blank lines between the segments and i solved it liked this :
content = content.replace(/^\s*\n/gm, '');
Note: This will just remove blank lines. You need to still figure out how to get the next line on current line
You can try regex to eliminate all '\n' not followed by any segment.

SyntaxError: unterminated string literal in PHP variable

I search through the numerous questions already asked about the "unterminated string literal" syntax error but found nothing helping me ...
So, I have this Javascript function :
function parametresModal($parametres) {
document.getElementById('remodalTest').innerHTML = $parametres;
};
Then I call this function on a link in my page :
<a href="#" onClick='parametresModal("<?php the_field('description-defi'); ?>");'>TEST</a>
The parameter written here is simplified ; I actually want to add this Wordpress ACF's function among others and HTML markup, but I found the issue was appearing with this particular field (see below).
This "parametresModal" function is supposed to fill the following div with its parameters :
<div id="remodalTest">MyDiv</div>
Problem is the console outputs
"SyntaxError: unterminated string literal"
The Wordpress ACF's field "description-defi" contains a few lines of text with some simple quotes (ex. c'est, l'éviter, ...).
So I tried to escape the quotes with several methods :
$myField = the_field('description-defi');
$myEscape = json_encode($myField);
or
$myField = the_field('description-defi');
$myEscape = addshlashes($myField);
or
$myField = the_field('description-defi');
$myEscape = htmlspecialchars($myField);
Always resulting in the same error.
Do you see where I could be wrong in my code or my way of thinking the thing ?
Thank you very much !
the_field() will output the content of the selected field. If you want to work with a field, you should use get_field() instead.
See: https://www.advancedcustomfields.com/resources/the_field/
Also the newline character will not be escaped by any of PHP's escape functions, if your String contains newlines, you will need to escape them manually using something like this: $myField = str_replace(array("\r\n", "\n"), "<br>", $myField);.
If you know that your DB will consistently use the same newline sequence, you can replace array("\r\n", "\n") by that newline sequence instead.

New line characters Desktop vs Server - Updated

I am trying to catch new line characters with JavaScript.But here is the catch.
For example with a string as argument to my function;
s = "some_text_with_a_\n_new_line";
Document.write(s);
It will get detected by my script;
Now if I get my text from a textarea and I press enter to produce a line break. It will get detected and interpreted as \n. Now the texte area and the working script are on my desktop. Window 10.
When i upload it to the server (IIS 7 on windows "Godaddy") its not working anymore so I have try some variant like \r\n , \r non of them work. Actually it dont mater much to me cause all I'm looking for is the escaping .
Here is a piece of script I use to find them:
nex = string.indexOf(tokout[i]); // where tokout = '\n'
spacer = 1; //spacer to escape found token
spc = tokin.length; ///last token
if(tokin[spc-1] === '\n'){ //// spacer to set cursor after token
spacer = 2;
}
BBCode = input.substr(curseur,nex+spacer); // we have bbcode
The script is working on my desktop but on the server I can't get the line break!
How it works:
I get the position of the \n.
Set the position of my cursor right after it.
and what is in BBcode is the line break.
For some reason it only work on my PC not the server. So I'm thinking it has to be the way break line are interpreted by the server!
They are not interpretted as space. They are displayed as space. To display AS IS you could surround with a pre tag. e.g.
document.write('<pre>' + s + '</pre>');
Also, you should be looking for \n not //n
i.e. s.indexOf('\n')
if you are trying to output to a document, you could replace the \n with <br/>
e.g. s.replace(/\n/g, '<br/>')
If you want to see line break in HTML you have to use <br /> HTML tag:
document.write("some_text_with_a_<br />_new_line");
However in javascript \n is a valid line break character. For example when you use console.log or alert you will see 2 lines:
console.log("some_text_with_a_\n_new_line");
alert("some_text_with_a_\n_new_line");

javascript regex invalid quantifier error

I have the following javascript code:
if (url.match(/?rows.*?(?=\&)|.*/g)){
urlset= url.replace(/?rows.*?(?=\&)|.*/g,"rows="+document.getElementById('rowcount').value);
}else{
urlset= url+"&rows="+document.getElementById('rowcount').value;
}
I get the error invalid quantifier at the /?rows.*?.... This same regex works when testing it on http://www.pagecolumn.com/tool/regtest.htm using the test string
?srt=acc_pay&showfileCL=yes&shownotaryCL=yes&showclientCL=no&showborrowerCL=yes&shownotaryStatusCL=yes&showclientStatusCL=yes&showbillCL=yes&showfeeCL=yes&showtotalCL=yes&dir=asc&closingDate=12/01/2011&closingDate2=12/31/2011&sort=notaryname&pageno=0&rows=anything&Start=0','bodytable','xyz')
In this string, the above regex is supposed to match:
rows=anything
I actually don't even need the /? to get it to work, but if I don't put that into my javascript, it acts like it's not even regex... I'm terrible with Regex period, so this one has me pretty confused. And that error is the only one I am getting in Firefox's error console.
EDIT
Using that link I posted above, it seems that the leading / tries to match an actual forward slash instead of just marking the code as the beginning of a regex statement. So the ? is in there so that if it doesn't match the / to anything, it continues anyway.
RESOLUTION
Ok, so in the end, I had to change my regex to this:
/rows=.*(?=\&?)/g
This matched the word "rows=" followed by anything until it hit an ampersand or ran out of text.
You need to escape the first ?, since it has special meaning in a regex.
/\?rows.*?(?=\&)|.*/g
// ^---escaped
regtest.htm produces
new RegExp("?rows.?(?=\&)|.", "") returned a SyntaxError: invalid
quantifier
The value you put into the web site shouldn't have the / delimiters on the regex, so put in ?rows.*?(?=\&)|.* and it shows the same problem. Your JavaScript code should look like
re = /rows.*?(?=\&)|.*/g;
or similar (but that is a pointless regex as it matches everything). If you can't fix it, please describe what you want to match and show your JavaScript
You might consider refactoring you code to look something like this:
var url = "sort=notaryname&pageno=0&rows=anything&Start=0"
var rowCount = "foobar";
if (/[\?\&]rows=/.test(url))
{
url = url.replace(/([\?\&]rows=)[^\&]+/g,"$1"+rowCount);
}
console.log(url);
Output
sort=notaryname&pageno=0&rows=foobar&Start=0

Categories

Resources