Illegal character in javascript - javascript

I've been debugging this for hours already but really can't find the culprit of this illegal character. My javascript looks fine. This is my code.
this.PrintApplication = function Test$PrintApplication(ApplicationID, callback) {
$.post("/Application/PrintApplication/" + ApplicationID,
function (data) {
var result = eval(data);
if (result.error) {
DisplayPrompt("Error", result.message);
return;
}
else {
callback(result.data);
}
});
};
In firebug it shows.
In inspect in chrome and in console it redirects me in this line.
Any idea where is that illegal character is in my function?

It looks like you've got some unprintable characters in your source. Do you have a way of displaying those in your editor and deleting them? Deleting and retyping the line might fix it as well.
If that's not the case, maybe what you're trying to evaluate isn't JavaScript at all. You could be running that on an image or some kind of binary data.
Remember to be extra super careful when using eval on data that comes from an external source. If you can avoid doing it, avoid it.

This might be due to the reason that you have copied the code from web and simply pasted in your file. Try typing the same code to the file.
This error occurs due to UTF-8 characters.

This could happen if you normally type with different alphabets. For example the Γreek question mark ; is a different ASCII character from the English semi colon ;. If you use the first, you'll get exactly this error.
One solution is to copy paste your method to notepad and then back to your IDE.
This will often normalise and eliminate weird characters that might be hidden or undecipherable.

Related

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.

Javascript regex to check for error tags

I need to write a regex that will tell me if any back-end framework that I'm working with is throwing an error and then store those errors in an array for retrieval if necessary.
The issue is, they use different tags for errors. Tags are as follows:
{{error}}, <<error>>, [[error]], and <{:error:}>
Usually, but not always, a set of braces will come after. Inside the braces will be a string; either an explanation of the error, or a JSON string containing more info about the error, like this:
<<error>> { Something has gone terribly wrong. }
<<error>> {
{"some":"json"}
}
<{:error:}> { What went wrong? }
As of now, it's undergoing a specific check for each tag, which is rather inefficient, like this:
if ( string.indexOf('<<error>>') >= 0 )
// Remove << and >>
if ( string.indexOf('[[error]]') >= 0 )
// Remove [[ and ]]
// So forth...
Then, I am left with a string like this:
error { Some description. }
or
error {
{"some":"json"}
}
Which I need a regex to extract what's between the brackets. This was the regex I wrote, but it falls short on numerous things:
string.match('/error\s?\{([^\}]+)\}/gi');
As I said, this procedure is very inefficient and has issues.
First, it doesn't allow the braces {} after error to be optional. They should be optional.
Second, it does not allow JSON as the charset [^}] is not matched when JSON presents it's closing}. So I need some way of matching all characters in a set until the opening bracket of error is closed. Is this possible?
Given the comments on my first answer, I'd use this regular expression as a replace to convert the data into single-line json, the regex also removes comments. It removes newlines that do not start with a properly labeled error. Multiline must be on.
(?:\/[\s\S*]*?\*\/|\/\/.*$|\s*^\s*(?!<<|{{|\[\[|<{:))) (demo)
or (?:\s*^\s*(?!<<|{{|\[\[|<{:)) if there are never comments to remove
And then this to extract the error information, on the reformatted string, this regex to match.
({{error}}|<<error>>|\[\[error\]\]|<{:error:}>)[ \t]*(?:(.*)}\s*$)? demo
I'll leave the other answer intact as I think it basically explains the problems that a person can encounter doing this.
Good question. Explained your problem, showed what you've tried, gave enough examples of input.
Regex, especially Javascript's limited implementation, is not ideal for parsing many languages and data objects. It can be difficult in this scenario to capture say 5. .* wants to go to 6 and .*? wants to go to 4.
{
{
{
} // 5
} // 5
} // 6
However, if your code is really indented like your examples (it may not be, that could be you making it readable), you should be able to use something like this ({{error}}|<<error>>|\[\[error\]\]|<{:error:}>)\s*(\s*{(.*?(?=$)|[\s\S]*?^)})?, (demo)
What this is doing is
capturing from { to } on the same line and if it can't, it proceeds to step 2 (alternation.
everything between { and } as long as } starts the line.
If the } is always prefixed by a certain number of spaces, you can prefix the marked } with that number of spaces in the regex.
({{error}}|<<error>>|\[\[error\]\]|<{:error:}>)\s*(\s*{(.*?(?=$)|[\s\S]*?^)})?`
^
If the } is always prefixed by the same number of spaces as the opening error marker, you can do this
([t ]*)({{error}}|<<error>>|\[\[error\]\]|<{:error:}>)(?:[ \t]*({(.*?(?=}$)|[\s\S]*?^\1)})?) (demo)
For this example, it's important to look at the full sample indent text. I demonstrate how it can go wrong.
If these won't work, you'll need a more code-oriented solution, but at the very least you can detect presence of errors with this
({{error}}|<<error>>|\[\[error\]\]|<{:error:}>)
. Chris85's simpler version is bad form, it could match <<error]] and any other combination, something he's probably aware of.

How to filter emojis from string jquery/javascript?

I'm using the following to exclude emojis/emoticons from a string in php. How do I do the same with javascript or jQuery?
preg_replace('/([0-9|#][\x{20E3}])|[\x{00ae}|\x{00a9}|\x{203C}|\x{2047}|\x{2048}|\x{2049}|\x{3030}|\x{303D}|\x{2139}|\x{2122}|\x{3297}|\x{3299}][\x{FE00}-\x{FEFF}]?|[\x{2190}-\x{21FF}][\x{FE00}-\x{FEFF}]?|[\x{2300}-\x{23FF}][\x{FE00}-\x{FEFF}]?|[\x{2460}-\x{24FF}][\x{FE00}-\x{FEFF}]?|[\x{25A0}-\x{25FF}][\x{FE00}-\x{FEFF}]?|[\x{2600}-\x{27BF}][\x{FE00}-\x{FEFF}]?|[\x{2900}-\x{297F}][\x{FE00}-\x{FEFF}]?|[\x{2B00}-\x{2BF0}][\x{FE00}-\x{FEFF}]?|[\x{1F000}-\x{1F6FF}][\x{FE00}-\x{FEFF}]?/u', '', $text);
This is what I try to do
$('#edit.popup .btn.save').live('click',function(e) {
var item_id = $(this).attr('id');
var edited_text = $('#edit.popup textarea').val().replace(/([0-9|#][\x{20E3}])|[\x{00ae}|\x{00a9}|\x{203C}|\x{2047}|\x{2048}|\x{2049}|\x{3030}|\x{303D}|\x{2139}|\x{2122}|\x{3297}|\x{3299}][\x{FE00}-\x{FEFF}]?|[\x{2190}-\x{21FF}][\x{FE00}-\x{FEFF}]?|[\x{2300}-\x{23FF}][\x{FE00}-\x{FEFF}]?|[\x{2460}-\x{24FF}][\x{FE00}-\x{FEFF}]?|[\x{25A0}-\x{25FF}][\x{FE00}-\x{FEFF}]?|[\x{2600}-\x{27BF}][\x{FE00}-\x{FEFF}]?|[\x{2900}-\x{297F}][\x{FE00}-\x{FEFF}]?|[\x{2B00}-\x{2BF0}][\x{FE00}-\x{FEFF}]?|[\x{1F000}-\x{1F6FF}][\x{FE00}-\x{FEFF}]?/u, '');
$('#grid li.image#' + item_id + ' img').attr('data-text', edited_text);
});
I found this suggestion in another post on Stack Overflow, but it's not working. It's still allowing emojis from ex ios.
.replace(/([\uE000-\uF8FF]|\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDDFF])/g, '')
What I try to achieve is to not allow emojis in textfield, and if an emoji is inserted (from ex ios keyboard) it will be replaced by nothing. It works with php. Someone here who can help me out with this?
Based on the answer from mb21, this regex did the job. No loop required!
/[\uD800-\uDBFF][\uDC00-\uDFFF]/g
As pointed out in this answer, JavaScript doesn't support Unicode code points outside the Basic Multilingual Plane (where iOS emojis lie).
I highly recommend reading The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!). Then you'll understand what was meant with:
So some indirect approach is needed. Cf. to JavaScript strings outside of the BMP.
For example, you could look for code points in the range [\uD800-\uDBFF] (high surrogates), and when you find one, check that the next code point in the string is in the range [\uDC00-\uDFFF] (if not, there is a serious data error), interpret the two as a Unicode character, and replace them by whatever you wish to put there. This looks like a job for a simple loop through the string, rather than a regular expression.

Javascript string variable unquoted?

I am using the QuickBlox JavaScript API. Looking through their code, I found this line:
var URL_REGEXP = /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/gi;
It appears that it has declared a string variable that is a regular expression pattern. Then it goes ahead to use that variable thus:
return str.replace(URL_REGEXP, function(match) {
url = (/^[a-z]+:/i).test(match) ? match : 'http://' + match;
url_text = match;
return '' + escapeHTML(url_text) + '';
});
I am wondering how is this possible? The var declared in the first line should be a string, but it is unquoted. Shouldn't this be a syntax error?
I went ahead and tested this code on my browser, and it works! This mean's I've got some learning to do here... Can anyone explain how this variable is declared?
Additionally, I tried to run the same code on my friends computer, the Chrome debugger throws a syntax error on the variable declaration line (unexpected token '/'). I am using Chrome Version 36.0.1985.143 m, my friend is using the same thing, but on my computer, it all works fine, on my friends computer, the code stops at the first variable declaration because of "syntax error".
Is there some setting that is different?
Any help would be appreciated.
UPDATE
Thanks for the quick answers. I've come from a PHP background, so thought that all regular expressions has to be initialized as strings :P.
Anyone can reproduce the syntax error I'm getting on my friends computer? (It still happens after disabling all extensions). I can't reproduce it either, and that's what is frustrating me.
UPDATE 2
I have tested and my friends computer and looked through the source. It appear to be due to some encoding problems (I'm not sure what). The regular expression is shown like this:
var URL_REGEXP = /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?芦禄鈥溾€濃€樷€橾))/gi;
(The characters at the end of the code is some random chinese characters, it seems).
How can I change the encoding to match his browser/system? (He is running on a Windows 7 Chinese simplified system).
It is not a String variable. It is a regular expression.
Calling var varname = /pattern/flags;
is effective to calling var varname = new RegExp("pattern", "flags");.
You can execute the following in any browser that supports a JavaScript console:
>>> var regex = /(?:[\w-]+\.)+[\w-]+/i
>>> regex.exec("google.com")
... ["google.com"]
>>> regex.exec("www.google.com")
... ["www.google.com"]
>>> regex.exec("ftp://ftp.google.com")
... ["ftp.google.com"]
>>> regex.exec("http://www.google.com")
Anyone can reproduce the syntax error I'm getting on my friends computer? (It still happens after disabling all extensions). I can't reproduce it either, and that's what is frustrating me.
According to RegExp - JavaScript documentation:
Regex literals was present in ECMAScript 1st Edition, implemented in JavaScript 1.1. Use an updated browser.
No, it shouldn't be a syntax error. In Javascript, RegExp objects are not strings, they are a distinct class of objects. /.../modifiers is the syntax for a RegExp literal.
I can't explain the syntax error you got on your friend's computer, it looks fine to me. I pasted it into the Javascript console and it was fine.

Newline \n problem in JS

I am reading a file with xmlHttp object and splitting the responseText with newlines by split method.
But "\n" character literally doesn't work. It acts like an error in my code and causes my code not even function.
Here is the line:
var lines=myPlaylist.responseText.split("\n");
There is no error if I split the array myPlaylist with other characters.
Just \n causes problem which I fail to understand.
At first, I thought the error was due to white-space:nowrap since I execute my code on Chrome.
Though I never used white-space in anywhere, I tried to set it to normal but it didn't work.
Similarly, I tried my code on other browsers (Firefox, IE etc), it didn't work either. Looks like I have a problem with using \n. Is there any other way to use newline or error with my code?
And by the way, error seems to be a syntax error since it does not just ignore \n character. Simply causes my code not to work
EDIT: An example responseText
[playlist]
File1=http://localhost:7000/Videos/Big%20Buck%20Bunny%20Trailer.ogv
Title1=Bunny Trailer
Length1=23
File2=http://localhost:7000/Videos/Dizzy%20Cat%20Video.ogv
Title2=Kedi
Length2=33
NumberOfEntries=2
Version=2
I found my own solution to my problem.
After using random special characters, \r character used for carriage return worked like a charm for my problem.
It acted like a newline \n character or at least it did its job in my case.
Thanks everyone for answers and helpful comments.
Try using this line
/\n/
Instead of this one
"\n"
Here's an SO thread that will provide a bit more insight
JavaScript string newline character?
EDIT1:
I just tested this and it splits appropriately on new lines. Can you post some of what you're trying to split?
<html>
<script>
function testSplit(value)
{
var lines = value.split(/\n/);
alert(lines);
}
</script>
<body>
<textarea id="test" name="test" onblur="testSplit(this.value);">
</textarea>
</body>
</html>
EDIT2:
Can you try converting your responseText to an object and seeing what you get from it - sorry, just shooting from the hip here since I haven't had time to mock up anything for testing.
eval("var playlistResponse = ("+ myPlaylist.responseText +")");
Here's a somewhat old article that might be useful to you: http://www.peachpit.com/articles/article.aspx?p=443580&seqNum=4
Use \\n instead of \n i tried on my code and it is working fine
This should work without problem.
Are you certain that myPlaylist has a responseText property, and that property is a string?
What happens if you catch an eventual error?
try {
var lines = myPlaylist.responseText.split(/\n/g);
alert(lines.length);
} catch (e) {
alert(e.message);
}

Categories

Resources