The price are render like this on my website : 20$US , how can I remove the US symbol and keep the $ symbol with regex (JavaScrip) ?
I would like the price to be render like this :20$
I have tried this :
<script>
$.each($('.price'), function() {
var pri = $(this).html();
$(this).html(pri.replace(/\D/g,''));
} )
</script>
Any idea ?
You should use replace method which accepts as first parameter a regex expression.
The replace() method returns a new string with some or all matches of
a pattern replaced by a replacement. The pattern can be a string or a
RegExp, and the replacement can be a string or a function to be called
for each match.
let string='20$US';
let desired = string.replace(/US/gi, '');
console.log(desired);
Related
I need regex to match a condition like below:
if ev or pp is in the begining of string and has any number after that,then it should match
For example:
if string is ev100 then it will satisify the condition so it should print ev100.
if string is pp44 then print pp44.
if string is ep39 then it will not satisfy the condition. Hence it should not be printed
You may use match here with the regex pattern ^(?:ev|pp)\d+:
var inputs = ["ev100", "pp44", "ep39"];
inputs.forEach(x => x.match(/^(?:ev|pp)\d+/) ? console.log(x) : "");
var data = this.state.registerMobile;
//My data will be like +91 345 45-567
data.replace('-','');
It is not removing '-' and i am trying to remove spaces also in between.It's not working.
For that, you need to assign the result of replace to some variable, replace will not do the changes in same variable, it will return the modified value. So use it like this:
var data = this.state.registerMobile;
data = data.replace('-', '');
console.log('updated data', data);
Check the example:
a = '+91 12345678';
b = a.replace('+', '');
console.log('a', a );
console.log('b', b );
String.prototype.replace() does not change the original string but returns a new one. Its first argument is either of the following:
regexp (pattern)
A RegExp object or literal. The match or matches are replaced with newSubStr or the value returned by the specified function.
substr (pattern)
A String that is to be replaced by newSubStr. It is treated as a verbatim string and is not interpreted as a regular expression. Only the first occurrence will be replaced.
So if you want to replace hypens and whitespaces, you have to use the following:
var data = this.state.registerMobile;
data = data.replace(/\s|-/g, '');
Let's say I have a string:
"__3_"
...which I would like to turn into:
"__###_"
basically replacing an integer with repeated occurrences of # equivalent to the integer value. How can I achieve this?
I understand that backreferences can be used with str.replace()
var str = '__3_'
str.replace(/[0-9]/g, 'x$1x'))
> '__x3x_'
And that we can use str.repeat(n) to repeat string sequences n times.
But how can I use the backreference from .replace() as the argument of .repeat()? For example, this does not work:
str.replace(/([0-9])/g,"#".repeat("$1"))
"__3_".replace(/\d/, function(match){ return "#".repeat(+match);})
if you use babel or other es6 tool it will be
"__3_".replace(/\d/, match => "#".repeat(+match))
if you need replace __11+ with "#".repeat(11) - change regexp into /\d+/
is it what you want?
According https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
str.replace(regexp|substr, newSubStr|function)
and if you use function as second param
function (replacement)
A function to be invoked to create the new substring (to put in place of the >substring received from parameter #1). The arguments supplied to this function >are described in the "Specifying a function as a parameter" section below.
Try this:
var str = "__3_";
str = str.replace(/[0-9]+/, function(x) {
return '#'.repeat(x);
});
alert(str);
Old fashioned approach:
"__3__".replace(/\d/, function (x) {
return Array(+x + 1).join('#');
});
Try this:
var str = "__3_";
str = str.replace(/[0-9]/g,function(a){
var characterToReplace= '#';
return characterToReplace.repeat(a)
});
Example of text:
Some string here : my value
Another string : my value
String : my value
I want to match everything before and including the symbol :
My wanted output is:
Some string here :
Another string :
String :
Thanks
Just use:
(.* :)
See example: https://regex101.com/r/bA1cQ1/2
Don't use a regular expression, because it's not a nail to regex's hammer.
var strToMatch = "Some string here : my value";
var match = strToMatch.slice(0,strToMatch.indexOf(':')+1);
// do something with the match
document.body.appendChild(document.createElement('pre')).innerHTML = match;
I have a string that look like this :
blablablablafunction tr(b){b=b.split("");b=b.reverse();b=b.slice(2);return b.join("")}blablablabla
And i want to get : b=b.split("");b=b.reverse();b=b.slice(2);return b.join("")
with Regex :
var match = "function tr(b){(.*)}";
var f = html.match(match);
And i get null in f.Any idea what is the problem?
You will have to escape special characters in the regex in this case I believe these are { , } and also ( and )(around the function argument list). Use the escape character(\) to do that. So try this regex:
var match = "function tr\\(b\\)\\{(.*)\\}";