extracting text between two characters [duplicate] - javascript

This question already has answers here:
Regular expression to get a string between two strings in Javascript
(13 answers)
Closed 6 years ago.
Having a string like:
"*this* not that"
I can select *this*
\*(.*?)\*
but I'm not able to get only this.
what I am trying to achieve is to replace -this- by a new string. What's the easiest way to do that ?

you can try:
"*this* not that".replace(/\*.*\*/,'*new_string*');
//"*new_string* not that"

Related

How to get remaining values except matches in JavaScript Regex Match [duplicate]

This question already has answers here:
Split string into array without deleting delimiter?
(5 answers)
JS string.split() without removing the delimiters [duplicate]
(10 answers)
Closed 2 years ago.
String ${provider.name} - ${item.publication_time|date|relative}
Regex /\${([^}]+)}/gmi
From string.match(/\${([^}]+)}/gmi) this, I am getting ["${provider.name}", "${item.publication_time|date|relative}"] as output.
But I need other unmatched parts of the string in the output array. My expected result should be ["${provider.name}", " - ", "${item.publication_time|date|relative}"]
Can you please suggest me how can I achieve this by changing the regex?

How to replace a string in javascript only when it's comes before another string using regex? [duplicate]

This question already has answers here:
REGEX: Capture Filename from URL without file extension
(5 answers)
JavaScript Regexp to replace a value only when it follows certain characters?
(2 answers)
Closed 3 years ago.
I want to replace xx with . in this string 100x100/<name>xx-somename-xxjpg but only xx comes next to jpg.
'100x100/<name>xx-somename-xxjpg'.replace(/(xx(jpg|jpeg|png))/,'$')
What I get -> 100x100/<name>xx-somename-$
What I expect-> 100x100/<name>xx-somename-.jpg
How do I do it?
'100x100/<name>xx-somename-xxjpg'.replace(/(xx)(?=jpg|jpeg|png)/,'.')

Replace dot in a number [duplicate]

This question already has answers here:
Removing everything except numbers in a string
(7 answers)
Closed 3 years ago.
I want to replace the dot in a number in Javascript with regular expression; if country_temp is, for example, 1.234, how can I put the value 1234 in country_temp2?
I have tried the following:
const country_temp2 = country_temp.replace(/[a-zA-Z]*\**\s*\./g, "")
but it's not working.
Edit: the original string is composed by characters, asterisk, a number and a dot
Sorry, but I have written in a very fast way.
Try this:
country_temp.replace(/\./g,'')

Replacing “index” array string using Javascript regex [duplicate]

This question already has answers here:
Regex using javascript to return just numbers
(14 answers)
Closed 6 years ago.
with the following code can get the 1 in the string.
var match = /[0-9]+/.exec('[1][2]');
console.log(match);
How do i get the 2 and not the 1 ?
Try with this regex
console.log(/(\[[0-9]\])+/.exec('[1][2]'));
I think it's only a matter of escaping the square brackets

How do I get the last character with jquery [duplicate]

This question already has answers here:
How to get the last character of a string?
(15 answers)
Closed 8 years ago.
I have a string I am trying to get the last character with jquery. How would I do that?
var stringTemp = "fieldNameWithIndex_1";
stringTemp.charAt(stringTemp.length-1);
or
stringTemp.slice(-1);
Use .split():
stringTemp.split('_')[1]

Categories

Resources