using a non literal as a template string in javascript [duplicate] - javascript

This question already has answers here:
Convert a string to a template string
(22 answers)
Closed 7 years ago.
Given this?
var v = 10;
var template = "testing ${v}...";
How do I achieve the same result as the following template string.
var result = `testing ${v}...`;
The template string is stored in a variable, and not a literal template string.
Is there a method to apply a template string when the template string is stored in a variable, and not literal?

var v = 10;
var template = "testing ${v}...";
var output = template.replace("${v}", `${v}`);
alert(output);

Related

How i can use variable in string? [duplicate]

This question already has answers here:
Template literals like 'some ${string}' or "some ${string}" are not working
(7 answers)
Closed 1 year ago.
I have problem with template strings. I created a variable, but i can't put it in string
let suffix = "-dev";
alert("hello, ${suffix}");
To use template strings you have to use backticks ( `` ).
let suffix = "-dev";
alert(`hello, ${suffix}`);

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

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...

Replace keywords from text using javascript [duplicate]

This question already has answers here:
Format a JavaScript string using placeholders and an object of substitutions?
(17 answers)
Closed 4 years ago.
How can I replace values from a string like that:
// "Hello ##name##, today is ##date##"
It's possible like that:
var string = "Hello ##name##, today is ##date##"
console.log(string.replace('##name##', 'John Doe'));
But how replace the ##date##too, and build the string again?
You would use a regex and pass a function as a second argument:
var string = "Hello ##name##, today is ##date##";
const map = {name: 'Foo', date: 'bar'};
console.log(string.replace(/##(\w+)##/g, (_,m) => map[m]));

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 include a variable value into regex pattern? [duplicate]

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Regular Expression Pattern With A Variable
(4 answers)
Closed 8 years ago.
I want to include a value of variable into regex pattern.
The following code is the familiar.
var regex = /(.)\1{2}/;
I want to do like The following.
var number = 2;
var regex = /(.)\1{number}/
How to do that ?
var number = 2;
var regex = new RegExp('(.)\\1{'+number+'}');

Categories

Resources