How i can use variable in string? [duplicate] - javascript

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

Related

JavaScript/regex: Remove text between brackets in string [duplicate]

This question already has answers here:
How to strip HTML tags from string in JavaScript? [duplicate]
(4 answers)
Closed 2 years ago.
How would I remove all <> tags from a string, and the contents within them?
<p>Hello, how are you doing <strong>today</strong></p>
to
Hello, how are you doing today
using JavaScript with Regex?
Here is how you can do it:
const input = '<p>Hello, how are you doing <strong>today</strong></p>';
const result = input.replace(/<[^>]+>/g, '');
console.log(result);

How can I pass the variable to the to RegEx? [duplicate]

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Closed 2 years ago.
I want to pass the cookie as a variable .
letstr = cookie;
(/\bcookie\b/g).test('having cookie.');
Here you can pass the variable to the regEx
let x = "cookie";
let reg = new RegExp(\\b${x}\\b,'g');
x.test("having cookie");
\b will create /\b this. and let 'g' will be added to the end of the string

JavaScript RegExp concatinating variable [duplicate]

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Why this javascript regex doesn't work?
(1 answer)
Closed 4 years ago.
Trying to replace all a's in the string ignoring the case.
str = "All a's will be replaced";
str.replace(/a/gi, 'Aa');
As expected, the above line of code produces Aall Aa's will be replAaced
But,
regExp = new RegExp('/a/gi');
str.replace(regExp, 'Aa');
the above line with the use of RegExp pattern produces original string All a's will be replaced
How to achieve expected result?

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

I need How to split this string in javascript? [duplicate]

This question already has answers here:
Regular Expression to find a string included between two characters while EXCLUDING the delimiters
(13 answers)
Closed 5 years ago.
var faxNum = $("#txt_faxnum").val();
//Value : ""kekurt kekt809" <(732) 588-8300>"
I want to split between "< >". Example: Just I need to this section : (732) 588-8300
How to split this string ?
You can try this
var String=faxNum.substring(str.lastIndexOf("<")+1,faxNum.lastIndexOf(">"));

Categories

Resources