Replace keywords from text using javascript [duplicate] - javascript

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

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

can't get second number between parenthesis using regex [duplicate]

This question already has answers here:
How do you access the matched groups in a JavaScript regular expression?
(23 answers)
Closed 5 years ago.
now I begin learning regex, and I have set of string in format like "(9/13)", and I need get second number. I try this regex: /\(.*?[^\d]*(\d+?)\)/g, in online regex it works normally.
But here:
var d = "(9/13)";
var v = /\(.*?[^\d]*(\d+?)\)/g;
alert(d.match(v));
it returns "(9/13)" , what am I doing wrong?
const source = "(9/13)";
const re = /\/(\d+)\)/;
console.log('result', re.exec(source).pop())
you can use Regex.exec() to find the number
const source = "(9/13)";
const re = /\(\d+\/(\d+)\)/;
console.log(re.exec(source)[1])

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

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

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