This question already has answers here:
Creating multiline strings in JavaScript
(43 answers)
Closed 7 years ago.
EDIT OK, I've realised there is already an answer answering this question with NO: Creating multiline strings in JavaScript
Thanks for all escaping/concatenating answers, but it is not what I needed.
END EDIT
In Python it is possible to define string variables having many lines by the notation
"""
many
many lines
"""
Is there something like this in JavaScript?
I think you could use it like this:
var str = 'many\n' +
'many lines';
Although such a thing does not exist in Javascript there is a way around it. Instead of the occasional """ """, you can add a \ at the end of each line. That in turn will create a "Multi-line string". Here is an example:
var myString = "This is \
my multi \
line string";
EDIT
I thought I should also point this out. Another way of accomplishing this is to concatenate together the strings, like so:
var myString = "This is" +
"my multi" +
"line string";
Related
This question already has answers here:
Does JavaScript have literal strings?
(6 answers)
Javascript - How to show escape characters in a string? [duplicate]
(3 answers)
Closed 3 years ago.
In C# I can do the following:
String1 = "Test \r\n test!";
String2 = #"Test \r\n test!";
output String1:
Test
test!
output String2:
Test \r\n test!
In JavaScript I only found unescape(). But that is completly outdated and is not really what I was searching for, because that translated special characters to other things. I want that 'nothing' is translated, but everything is given out as it was in the string. Has someone an idea how I can do in JS what I can achieve in C# with the '#'?
JavaScript has no equivalent to C# verbatim string literals.
You need to escape special characters when creating the string.
const string1 = "Test \\r\\n test!";
Some people have suggested using JSON.stringify, but the initial parsing of the string will normalise it, so you can't reliably recover the original input.
For example, an escaped space means the same as a space on its own.
const input = "A string containing a \ character";
const output = JSON.stringify(input);
document.write(output);
This question already has answers here:
How to use split?
(4 answers)
Closed 6 years ago.
I have a blog and blog titles are like this;
"Hey There: Part 1"
So, is there any chance to break the line after ":", because I want Part 1 to start a new line. So it should be like this:
"Hey there:
Part 1"
But there are so many titles, so I want to do this with a javascript code to all of those titles. Is this possible?
You can use split() of JavaScript
var text = "Hey There : Part 1";
var newText = text.split(":").join('\n');
alert(newText);
Using jQuery:
var blogTitle = $('theTitleElement')
$(blogTitle).each(function(){
$(this).html(this.textContent.split(':').join('<br>'))
});
%0D%0A are the characters for a carriage return and line break if you're URL encoding.
%0D is a carriage return character %0A is a line break character.
If you want to line break in your JavaScript code then you can use \n to escape a line break.
var sOrig = "Hey There: Part 1";
sOrig.split(':').join('\n');
you can replace the \n with html etc if needed or just do a replace()
This question already has answers here:
How do I replace an asterisk in Javascript using replace()?
(6 answers)
Closed 7 years ago.
I am trying to replace the same string *a*a consistently with *a.
Tried many variations of something like this, but none really worked:
s = s.replace( /\b*a*a\b/g, "*a");
So far running this leads to all xzy*a being replaced with xyz
* is a special regex character. If you want to match only an actual asterisk, then you have to escape it like this:
s = s.replace( /\*a\*a/g, "*a");
Working demo: http://jsfiddle.net/jfriend00/gvgshwyz/
An asterisk is a special regex character.
You just have to escape it like this: \*a in place of *a
This question already has answers here:
Are double and single quotes interchangeable in JavaScript?
(23 answers)
Closed 8 years ago.
I am building a website that teaches people how to code a website. I am trying to add a feature where they code the exercise into a text box and I then compare that to a string to see if they got it right or not yet. I am running into an issue though when there is quotes inside the strings answer because that then ends the string thus cutting off some of the answer. How can I get around this?
All feedback is greatly appreciated!
Here is an example of a strings answer that screws it up:
var answer = "var greeting="Hello World!"; ";
The second pair of quotes end the string's declaration early. Is there a way to include all of it including the second pair of quotes in the declaration?
You can:
Escape the quotes with \:
var answer = "var greeting=\"Hello World!\"; ";
var answer = 'var greeting=\'Hello World!\'; ';
Use different quotes:
var answer = "var greeting='Hello World!'; ";
var answer = 'var greeting="Hello World!"; ';
This question already has answers here:
How to split a long regular expression into multiple lines in JavaScript?
(11 answers)
Closed 8 years ago.
This one seems like it has a very simple answer, yet I can't find it anywhere. I have a regular expression that is quite large, how do I put in some line breaks in the expression itself so I don't have to keep scrolling horizontally through the code to see it all?
I don't normally use word-wrap, and the IDE I'm using doesn't even offer it anyway.
A line break in a string would normally be a \ at the end of the line :
var mystring "my string \
is on more \
than one line";
var re = new RegExp(mystring, "gim");
You could use RegExp and .join() to convert and concat a string.
var myRegExp = RegExp(['/^([a-zA-Z0-9_.-])+'
,'#([a-zA-Z0-9_.-])+'
,'\.([a-zA-Z])+([a-zA-Z])+/'].join(''));
The answer has been linked to here as well.
How to split a long regular expression into multiple lines in JavaScript?