Prevent back slash escaping from the string in Javascript [duplicate] - javascript

This question already has answers here:
Escaping backslash in string - javascript
(5 answers)
Closed 3 years ago.
I have a code which replaces the back slashes as empty string
var data = '<strong>Welcome</strong>\(x = {-b';
document.write(data);
I am getting result like this:
Welcome(x = {-b
I am expecting result like this without modifying string value
Welcome\(x = {-b
I used the same string to display in an id as html content.
document.getElementById("test").innerHTML = '<strong>Welcome</strong>\(x = {-b';
It always replacing slashes by empty string. the string.split method did not helped me to solve this

var data = '<strong>Welcome</strong>\\(x = {-b';
document.write(data);
try this...

Related

Extract text from enclosing parentheses using JavaScript [duplicate]

This question already has answers here:
Get text between two rounded brackets
(7 answers)
Closed 2 years ago.
I have a string like Manila (Philippines) and want to replace it with only the substring Philippines. I tried using the following regex pattern, which works in Notepad++:
[^\(]+ \(([^\)]+)\)
However, I get an undefined result in JavaScript:
var x = "Manila (Philippines)";
console.log(x.replace(/[^\(]+ \(([^\)]+)\)/,$1));
You just forgot the " around your replace pattern!
console.log(x.replace(/[^\(]+ \(([^\)]+)\)/,"$1")); will work correctly!
You can use .match():
var x = "Manila (Philippines)";
var result = x.match(/\((.+)\)/).pop();
// regex for string contained in parentheses
console.log(result);

Javascript Trying to parse string into an array [duplicate]

This question already has answers here:
Parsing string as JSON with single quotes?
(10 answers)
Closed 2 years ago.
I am having a variable which stores a string like this
var colorArr="['#3f67c5', '#cb4728', '#f19d39', '#459331', '#984830', '#8C2094']"
I am trying to convert this string into an array by
var result = JSON.parse(colorArr)
But I keep on getting the following error
"SyntaxError: Unexpected token ' in JSON at position 1
Is there a way by which I can convert this string into a proper array?
Thanks in advance
Use single quotes and put double quotes inside of it
var colorArr= '["#3f67c5", "#cb4728", "#f19d39", "#459331", "#984830", "#8C2094"]';
var result = JSON.parse(colorArr)
console.log(result)
You can use regex to replace and split.
let colorArr="['#3f67c5', '#cb4728', '#f19d39', '#459331', '#984830', '#8C2094']";
let myArray = colorArr.replace(/'/g,'"')
console.log(JSON.parse(myArray))
try like this
var colorArr='["#3f67c5", "#cb4728", "#f19d39", "#459331", "#984830", "#8C2094"]';
JSON.parse(colorArr)

Retreive Information Around Key Words Javascript [duplicate]

This question already has answers here:
Regular Expression to get a string between parentheses in Javascript
(10 answers)
Closed 7 years ago.
Let's say I have a string that looks like this:
var string = "(This is an auto-generated string) and this is useless text I don't need";
What I would like to do is assign another variable the string INSIDE the parentheses so the second variable would look like this:
var selected_string = "This is an auto-generated string";
How would I retrieve data around the parentheses and then assign it to a variable in JavaScript?
Thanks!
You can use a regular expression to pull out any information between parens:
var regex = /\((.*?)\)/;
var matched = regex.exec(string);
var selectedString = matched[1];

How to extract numbers from string in Javascript using regex [duplicate]

This question already has answers here:
Find and get only number in string
(4 answers)
Closed 8 years ago.
I have the following string
/Date(1317772800000)/
I want to use a Javascript regular expression to extract the numerical portion of it
1317772800000
How is this possible?
That should be it
var numPart = "/Date(1317772800000)/".match(/(\d+)/)[1];
No need for regex. Use .substring() function. In this case try:
var whatever = "/Date(1317772800000)/";
whatever = whatever.substring(6,whatever.length-2);
This'll do it for you: http://regex101.com/r/zR0wH4
var re = /\/Date\((\d{13})\)\//;
re.exec('/Date(1317772800000)/');
=> ["/Date(1317772800000)/", "1317772800000"]
If you don't care about matching the date portion of the string and just want extract the digits from any string, you can use this instead:
var re = /(\d+)/;
re.exec('/Date(1317772800000)/')
["1317772800000", "1317772800000"]

How to replace "{ with { in string using Javascript? [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 8 years ago.
Can anyone tell me how to replace "{ with { using JavaScript?
Here is what I am trying to do:
string.replace(/\"\{/g, "{");
Your regex is fine. Don't forget that strings are immutable in javascript. The replace function doesn't change the receiver string but builds a new one.
So you must do
string = string.replace(/\"\{/g, "{");
In case you were using this directly on string you should use it on an instance of string. Not on string type.
(I know it sounds too trivial, but otherwise this code should have worked. :) )
var stringTypeVariable = 'some string "{ with target pattern';
var replacedVariable = stringTypeVariable.replace(/\"\{/g, "{");

Categories

Resources