I have a javascript script string :
var link=C:\test\pictures\myimage\upload\1464592985595_151.jpg
I want to get following 1464592985595_151.jpg
I am using this to split but getting error in java script
link= link.split("\");
Error: unterminated string literal
How can I solve this problem ?
your link needs to be wrapped in ' signs like this:
var link='C:\test\pictures\myimage\upload\1464592985595_151.jpg';
Also, \ is used to escape characters so you have to escape the escaping:
link= link.split("\\");
From here it's just a matter of selecting the last piece:
console.log(link[link.length -1]; //Outputs '1464592985595_151.jpg'
My suggestion to you would be to find a nice coding tool with syntax highlighting like visual studio code to help you catch these things.
Check It .
var link="C:/test/pictures/myimage/upload/1464592985595_151.jpg";
var linkarray=link.split("/");
var myimage= linkarray[linkarray.length-1];
alert(myimage);
Note- avoid to use backslash (\) in string, because javascript take it
as a escape character.
Related
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.
In my script I have:
xmlFile = "C:\Timelines\Data\AKM.XML"
alert(xmlFile)
The alert displays as :
C:TimelinesDataAKM.XML
The line defining xmlFile is written into the script by an external program. But I have tried deleting it, and rekeying with NotePad, with same result.
TIA
Andrew
Backslashes are used to escape special characters. As in this case none of them are followed by known escape codes, they are ignored.
To escape the backslash itself to show an actual backslash, repeat it:
xmlFile = "C:\\Timelines\\Data\\AKM.XML"
Try this
xmlFile = "C:\\Timelines\\Data\\AKM.XML"
Because \ will be take it as escape character
Im trying to match lines that have this format. Writing a static regEx is fine but I need to do this using 2 variables to create the regex dynamically.
I cant seem to get how to escape the forward brackets properly ive tried escaping them, not escaping them and even double escaping them (just for the heck of it) but fireBug shows the actual regEx being created the same no mater how I do itand it doesnt match my input.
Lines to match look like this:
9.SSRDOCSWSHK1/////23NOV96/M//YEUNG/WINSTON/JEREMY-5.1
What Ive tried:
var regString ='\\d{1,2}.SSRDOCS[0-9A-Z]{2}HK1[/]{5}\\d\\d[A-Z]{3}\\d\\d/[MF]//'+curGstNme+'([/A-Z]+)?-'+pax.slice(0,1)+'\.'
var namedGdocRegEx = new RegExp(regString,"g");
FireBug gives RegExp /\d{1,2}.SSRDOCS[0-9A-Z]{2}HK1[\/]{5}\d\d[A-Z]{3}\d\d\/[MF]\/\/CASTANEDA\/HAZEL([\/A-Z]+)?-1./g
---------------------------
var regString ='\\d{1,2}.SSRDOCS[0-9A-Z]{2}HK1[\/]{5}\\d\\d[A-Z]{3}\\d\\d\/[MF]\/\/'+curGstNme+'([\/A-Z]+)?-'+pax.slice(0,1)+'\.'
var namedGdocRegEx = new RegExp(regString,"g");
FireBug gives RegExp /\d{1,2}.SSRDOCS[0-9A-Z]{2}HK1[\/]{5}\d\d[A-Z]{3}\d\d\/[MF]\/\/CASTANEDA\/HAZEL([\/A-Z]+)?-1./g
---------------------------
var regString ='\\d{1,2}.SSRDOCS[0-9A-Z]{2}HK1[\\/]{5}\\d\\d[A-Z]{3}\\d\\d\\/[MF]\\/\\/'+curGstNme+'([\\/A-Z]+)?-'+pax.slice(0,1)+'\.'
var namedGdocRegEx = new RegExp(regString,"g");
FireBug gives RegExp /\d{1,2}.SSRDOCS[0-9A-Z]{2}HK1[\/]{5}\d\d[A-Z]{3}\d\d\/[MF]\/\/CASTANEDA\/HAZEL([\/A-Z]+)?-1./g
In regex you need to escape the DOTs since DOT will mean any character.
Use this regex:
regString ='\\d{1,2}\\.SSRDOCS[0-9A-Z]{2}HK1/{5}\\d\\d[A-Z]{3}\\d\\d/[MF]//'+ curGstNme + '([/A-Z]+)?-' + pax.slice(0,1) + '\\.';
Actually the problem was elsewhere. Apparently fireBug just shows the created regex (in the "watch" panel) with the escaping sashes visible. This made me think the regex was not being created correctly.
Your regex could be reduced like this:
var regString ='\\d{1,2}\\.SSRDOCS[0-9A-Z]{2}HK1/{5}\\d{2}[A-Z]{3}\\d{2}/[MF]//'+curGstNme+'([/A-Z]+)?-'+pax.slice(0,1)+'\.'
What did I change ?
\\d\\d can be replaced efficiently with \d{2}.
. replaced with \\. for matching exactly dot.
[/]{5} replaced by /{5}
This visually gives you:
I've got a file reference in JS and I need to parse it via regex. All as I want is to get the 'C' character that follows a backslash. Does anyone know why this doesn't work?
var str = "C:\Course\folder\file.txt";
str.match(/\\C/g);
If I run this in firebug or similar tool, I get nothing back.
Does anyone know why this doesn't work?
Because the string you've quoted doesn't contain backslashes. It has an invalid escape sequence (\C) resulting in just C and two formfeeds (\f), but no backslashes.
If you have actual backslashes, it works:
var str = "C:\\Course\\folder\\file.txt";
str.match(/\\C/g);
I am trying to use John Gruber's URL regex in Javascript but NetBeans keeps telling me there is a syntax error and illegal errors:
var patt = "/(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])
|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]
{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|
(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|
(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:
'".,<>?«»“”‘’]))/";
Anyone know how to solve this?
As others have said, it's the double quote. But alternatively, you can just write the regexp as a literal in javascript (but then you need to escape the forward slashes in lines 1 and 3 instead).
var regexp = /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/i;
I also moved the case-insensitive modifier to the end. Just because. (edit: Well, not just "because" - see Alan Moore's comment below)
Note: Whether you use a literal or a string, it has to be on 1 line.
put the whole expression in one line, and remove the quotes at the start and end so it looks like this var patt = /the-long-patttern/;, netbeans will still complain, but the browsers won't and thats what matters.
You should write it like this in NetBeans:
"(?i)\\b((?:[a-z][\\w-]+:(?:\\/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]"
+ "+[.][a-z]{2,4}\\/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))"
+ "+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))";