printing unchanged string in JS [duplicate] - javascript

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);

Related

How to backslash in number using javascript [duplicate]

This question already has answers here:
How do I put a single backslash into an ES6 template literal's output?
(2 answers)
Closed 4 months ago.
How to add backslash in a number dynamically in JavaScript.
I want output like this : '/(123) 456/-7890'
let number = '1234567890';
let test = `\(${number.substr(0,3)}) ${number.substr(3,3)}'\'-${number.substr(6,4)}`;
Backslash removed after getting the output '(123) 456-7890'
You need to double the \ because it is a special escape character.
let number = '1234567890';
let test = `\\(${number.substr(0,3)}) ${number.substr(3,3)}'\\'-${number.substr(6,4)}`;

Javascript string search double and single quote using RegExp [duplicate]

This question already has answers here:
How do I do a case-insensitive string comparison?
(15 answers)
javascript includes() case insensitive
(11 answers)
Closed 2 years ago.
I'm using the following code to search sub string in a string
mystring.search(new RegExp(substring, 'i'))
Reason why I am using new RegExp is, I want to search case insensitive. However, when there is a string like
var mystring = '10" stick';
and I want to search 10", the code above does not return any result. It's clearly because of new RegExp and double quote. Is there any particular flag that needs to be passed in new RegExp? I googled a lot but couldn't find any solution. What am I missing?
search returns match position, maybe ur confused by this.
So, try this out,
const myString = '10" stick';
console.log(myString.match(new RegExp('10"', 'i'))[0])
console.log(myString.match(new RegExp('ick', 'i'))[0])
console.log(myString.match(new RegExp('asd"', 'i'))) // non-matching
It returns the match or null if theres non. AND it matches 10" in 10" stick
see String.prototype.search mdn

How to replace all / with - from string [duplicate]

This question already has answers here:
Replace forward slash "/ " character in JavaScript string?
(9 answers)
Why this javascript regex doesn't work?
(1 answer)
Closed 4 years ago.
I have a string field 01/01/1986 and I am using replace method to replace all occurrence of / with -
var test= '01/01/1986';
test.replace('//g','-')
but it does't give desire result. Any pointer would be helpful.
You just have a couple issues: don't put the regex in quotes. That turns it into a string instead of a regex and looks for that literal string. Then use \/ to escape the /:
var test= '01/01/1986';
console.log(test.replace(/\//g,'-'))
A quick way is to use split and join.
var test= '01/01/1986';
var result = test.split('/').join('-');
console.log(result);
Note too that you need to save the result. The original string itself will never be modified.

Replace '-' with '--' in a JavaScript string [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 4 years ago.
I am trying to replace a single dash '-' character in a string with double dashes.
2015–09–01T16:00:00.000Z
to be
2015-–09-–01T16:00:00.000Z
This is the code I am using but it doesn't seem to be working:
var temp = '2015–09–01T16:00:00.000Z'
temp.replace(/-/g,'--')
In JavaScript Strings are immutable. So, when you modify a string, a new string object will be created with the modification.
In your case, the replace has replaced the characters but returns a new string. You need to store that in a variable to use it.
For example,
var temp = '2015–09–01T16:00:00.000Z';
temp = temp.replace(/–/g,'--');
Note The string which you have shown in the question, when copied, I realised that it is a different character but looks similar to – and it is not the same as hyphen (-). The character codes for those characters are as follows
console.log('–'.charCodeAt(0));
// 8211: en dash
console.log('-'.charCodeAt(0));
// 45: hyphen
The hyphen character – you have in the string is different from the one you have in the RegExp -. Even though they look alike, they are different characters.
The correct RegExp in this case is temp.replace(/–/g,'--')
Probably the easiest thing would be to just use split and join.
var temp = '2015–09–01T16:00:00.000Z'.split("-").join("--");

JS regular expression multi-line [duplicate]

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?

Categories