Enforcing Line Breaks \n In Objective C - javascript

In Objective C I'm using the loadHTMLString to load a HTML string that has been inserted with a variable for JS. However, the string \n is not being included in the script.
For example, the code to be included:
'Product:A\nProduct:B\nProduct:C'
into java script as:
var source = 'Product:A\nProduct:B\nProduct:C'
However, in the completed HTML file the created script appears as:
var source = 'Product:A Product:B Product:C '
I need it to appear exactly as written for it to work. There are no substitutions:
var source ='Product:A\nProduct:B\nProduct:C'
Originally, I felt the problem was with formatting using: NSString stringWithFormat, but went with stringByAppendingString, but no luck - yet.
Thanks for your help
This is how the code appears when inserted by Obj C:
</head>
<body>
<h1>Absence Of Blood</h1>
<canvas id="target-canvas"></canvas>
<script>
var canvas = document.getElementById('target-canvas')
var source = 'Product:A
Product:B
Product:C
'
nomnoml.draw(canvas, source)
</script>
</body>
Obviously, it can only appear like this if code is entered in manually:
</head>
<body>
<h1>Absence Of Blood</h1>
<canvas id="target-canvas"></canvas>
<script>
var canvas = document.getElementById('target-canvas')
var source = 'Product:A\nProduct:B\nProduct:C'
nomnoml.draw(canvas, source)
</script>
</body>
I think Objective C is formatting the \n into linefeed.
A simply fix could be replace \n with ^ in a var and use javascript function to read the code and replace the ^ with \n in var and feed it to: nomnoml.draw(canvas, modifiedSource)
Or find a way to STOP Obj-C from converting \n into linefeeds.
A list of NSString options are here:
NSStrings
but I didn't see anything that would make a difference.

It seems like you want the literal text \ and n to appear in the output. To do this you need to escape the \n in your Objective-C strings.
So instead of something like this:
NSString *output = #"var source = 'Product:A\nProduct:B\nProduct:C'";
you need to escape the backslashes:
NSString *output = #"var source = 'Product:A\\nProduct:B\\nProduct:C'";
This will put a literal backslash followed by the letter "n" in the result instead of interpreting the \n special escape sequence as a newline character.

Related

Detect line breaks (\n) while reading from a database (Javascript, HTML, firebase)

I'm trying to use a string retrieved from firebase firestore and display it on my HTML page with line breaks.
In the store, I have a string that looks like this
row1\nrow2\nrow3
When I retrieve it and try to add it to my page the \n's do not register as line breaks. I am using pre-wrap and using a test-string works fine.
let text = getString(); //retrieves a string from firebase
document.getElementById('textBox').textContent = text;
Shows this on my page:
row1\nrow2\nrow3
The following test code:
let text = 'row1\nrow2\nrow3';
document.getElementById('textBox').textContent = text;
Shows the following on the page:
row1
row2
row3
So it seems like the \n's in the string retrieved from the database are not read the same way as the \n's that are put inside the string defined directly in the Javascript code. Any idea why? And what can I do to make it read the line breaks?
Similar to what #user14063792468 said in their comment, it appears your newline characters have been escaped (i.e., converts \n to \\n) when a string is stored, so I'd try to replace any instances of \\n with \n (single backslash) and see if that works.
Here's an example of how this might look before and after the replacement:
let string = 'Newline characters\\nare in this\\nstring\\nfor sure'; // note the double slashes
document.getElementById('before').textContent = string;
let text = string.replace(/\\n/g, "\n");
document.getElementById('after').textContent = text;
<pre id="before"></pre>
<pre id="after"></pre>

Javascript regex not containing keyword with backslashes

I'm having a problem with a javascript regex that has to comment out all tags inside a script tag. But it can not comment out special first script tag with id "ignorescript".
Here is a sample string to regex:
<script id="ignorescript">
var test = '<script>test<\/script>;
var xxxx = 'x';
</script>
Script tag inside ignorescipt has extra backslash because it is JSON encoded (from PHP).
And here is the final result i have to get:
<script id="ignorescript">
var test = '<!ignore-- <script>test<\/script> ignore-->;
var xxxx = 'x';
</script>
Following example works:
content = content.replace(/(<script>.*<\\\/script>)/g,
"<!--ignore $1 ignore-->");
But I need to check that it does not contain a keyword "ignorescript". If that keyword comes up then I do not want to replace anything. Otherwise add ignore comments to whole script tag So far I have gotten this far:
content = content.replace(/(<script.((?!ignorescript).)*<\/script>)/g,
"<!--ignore $1 ignore-->");
It kinda works, but not the way it supposed to be. I also have one more backslash in ending tag. So I changed it to:
content = content.replace(/(<script.((?!ignorescript).)*<\\\/script>)/g,
"<!--ignore $1 ignore-->");
Not it does not find anything at all.
Got it finally working.
Here is the working regex:
/(<script(?!\sid="ignorescript").*?<\\\/script>)/g

How do I replace line breaks?

I am trying to replace line breaks with a comma in javascript but the code doesn't seem to work.
var data = "Series
Manga
Games
Artbooks
Visual Novels"
var output = data.replace(/(\r\n|\n|\r)/gm,",");
alert(output);
here you can see a online version http://jsfiddle.net/CBvpS/
Anyone know how to fix it?
Works great when your input string is syntactically correct:
var data = "Series\nManga\nGames\nArtbooks\nVisual Novels"
var output = data.replace(/\r?\n/gm,",");
alert(output);
http://jsfiddle.net/7V8rg/1/
Javascript does not have multi-line variables like php does, unless you manually escape(\) the end of the line. Further, this does not count as a line-break, so you would have to insert \ns to fix that as well. Otherwise, your code works fine, albeit with some minor modifications.
var data = "Series\n \
Manga\n \
Games\n \
Artbooks\n \
Visual Novels";
var output = data.replace(/(\r\n|\n|\r)/gm,",");
alert(output);
Take note, however, if your data is from example, an input text area, you do of course not need to worry about escaping the end of the line, and it will handle the data as it should.
JavaScript doesn't allow you to continue a string with new lines unless you add a backslash at the end of the line. For example:
var string = "a \
string is \
here";
With that being said, if you retrieved some text from a different source and wanted to replace the new lines, something like this should be all you need:
string = string.replace(/\n/g, ',');

javascript to replace a text within [] with hyperlink

Am in a process of writing a javascript to replace a text within [] to a html link. But am stuck at generating a regular expression to match any string that is in [] and then replace it with a hyperlink.
Below is my code snippet that i have tried:
<html>
<head>
</head>
<body id="body">
Hello World [1234]<br>
[322]<br>
Hello
</body>
</html>
<script type="text/javascript">
var bodyText=document.getElementById('body').innerHTML;
var pattern = "\[(.*?)\]";
var replaceText = "Pradeep";
document.getElementById('body').innerHTML = bodyText.replace(pattern/gi, replaceText);
</script>
Can anyone please suggest me a best way to do it
Thanks,
If you have a string representing a regexp, you have to call new RegExp(str) to build a regexp from it, but you can just make a regexp literal here. Also, the i flag is not necessary since nothing in your regexp refers to letters. And, you don't use the capturing groups so you can eliminate them as well. Lastly, you need to escape the quotes in the string because the interpreter now thinks your strings ends after href=:
var bodyText = document.getElementById('body').innerHTML;
var pattern = /\[.*?\]/g;
var replaceText = "Pradeep";
document.getElementById('body').innerHTML = bodyText.replace(pattern, replaceText);
I tried \[[^\]]+\] as regexp and got the right output:
var s = 'Hello World [1234]<br>[322]<br>';
s = s.replace(/\[[^\]]+\]/ig, 'Pradeep');
// output
// Hello World Pradeep<br>Pradeep<br>

how to replace \ with \\ using javascript string functions

I have the following filepath
var imagepath="C:\Documents and Settings\Mahesh\Desktop\images\advts.png"
how can I replace \ with \ so so that it prints
var imagepath="C:\\Documents and Settings\\Mahesh\\Desktop\\images\\advts.png"
You need to use the replace() method whilst escaping the \ character on each occurrence:
imagepath = imagepath.replace(/\\/g, "\\\\");
//-> "C:\\Documents and Settings\\Mahesh\\Desktop\\images\\advts.png"
If imagepath is defined with single backslash characters, those will be evaluated into an escape sequence along with the character(s) following them. At this point it's too late to replace the backslashes as they are removed from the resulting string.
It's a string literal.. By the time it's assigned to imagepath, the escape sequences are already parsed and processed, and the backslashes no longer exist in the string. You just have to have them in the string in the first place.
To demonstrate what I mean:
>>> var imagepath="C:\Documents and Settings\Mahesh\Desktop\images\advts.png"
>>> imagepath
"C:Documents and SettingsMaheshDesktopimagesadvts.png"
There's no way to fix the issue at that stage.
You can use the string replace() method. Replace on W3Schools.com.
Example:
var imagepath="C:\Documents and Settings\Mahesh\Desktop\images\advts.png"
document.write(imagepath.replace("\\", "\\\\"));
Upate As reko_t said, once a path is assigned to imagepath, it's a processed literal. The \ characters disappears. I've just tested my code with this example and there's no way to fix it unfortunately.
<html>
<head>
<script type="text/javascript">
function init() {
var imagepath="C:\Documents and Settings\Mahesh\Desktop\images\advts.png"
alert(imagepath.replace(/\\/g, "\\\\"));
}
</script>
</head>
<body onload="init()">
</body>
</html>
The only way to do it, is to replace \ to \\ on server side, and assign it to imagepath before it gets rendered to javascript that needs to be used by javascript.
Just add after var imagepath initialisation
var imagepath=imagepath.replace("\", "\\");
return imagepath;
Try this :
str.replace(new RegExp('/', 'g'),'-')

Categories

Resources