JavaScript get number with comma from string - javascript

For example we have next string in javaScript
var str = "abc,de 55,5gggggg,hhhhhh 666 "
How i can get 55,5 as number?

It depends on what you can assume about your number, but for the example and a lot of cases this should work:
var str = "abc,de 55,5gggggg,hhhhhh";
var match = /\d+(,\d+)?/.exec(str);
var number;
if (match) {
number = Number(match[0].replace(',', '.'));
console.log(number);
} else console.log("didnt find anything.");

var str = "abc,de 55,5gggggg,hhhhhh"
var intRegex = /\d+((.|,)\d+)?/
var number = str.match(intRegex);
console.log(number[0]);

JS fiddle:
https://jsfiddle.net/jiteshsojitra/5vk1mxxw/
var str = "abc,de 55,5gggggg,hhhhhh"
str = str.replace(/([a-zA-Z ])/g, "").replace(/,\s*$/, "");
if (str.match(/,/g).length > 1) // if there's more than one comma
str = str.replace(',', '');
alert (str);

Related

How to remove string with specific value from a string using JavaScript? [duplicate]

This question already has answers here:
How to remove text from a string?
(16 answers)
Closed 5 years ago.
Suppose my string is like:
var str = "USA;UK;AUS;NZ"
Now from some a source I am getting one value like:
country.data = "AUS"
Now in this case I want to remove "AUS" from my string.
Can anyone please suggest how to achieve this.
Here is what I have tried:
var someStr= str.substring(0, str.indexOf(country.data))
In this case I got the same result.
var str = "USA;UK;AUS;NZ"
console.log(str + " <- INPUT");
str = str.split(';');
for (let i = 0; i < str.length; i++) {
if (str[i] == 'AUS') {
str.splice(i, 1);
}
}
console.log(str.join(';') + " <- OUTPUT");
You can use split and filter:
var str = "USA;UK;AUS;NZ"
var toBeRemoved = "AUS";
var res = str.split(';').filter(s => s !== toBeRemoved).join(';');
console.log(res);
Try this :
var result = str.replace(country.data + ';','');
Thanks to comments, this should work more efficently :
var tmp = str.replace(country.data ,'');
var result = tmp.replace(';;' ,';');
You can use replace() with a regex containing the searched country, this is how should be the regex /(AUS;?)/.
This is how should be your code:
var str = "USA;UK;AUS;NZ";
var country = "AUS";
var reg = new RegExp("("+country+";?)");
str = str.replace(reg, '');
console.log(str);
This will remove the ; after your country if it exists.
Here is a good old split/join method:
var str = "USA;UK;AUS;NZ;AUS";
var str2 = "AUS";
var str3 = str2 + ";";
console.log(str.split(str3).join("").split(str2).join(""));

replace first n occurrence of a string

I have a string var with following:
var str = getDataValue();
//str value is in this format = "aVal,bVal,cVal,dVal,eVal"
Note that the value is separated by , respectively, and the val is not fixed / hardcoded.
How do I replace only the bVal everytime?
EDIT
If you use string as the regex, escape the string to prevent malicious attacks:
RegExp.escape = function(string) {
return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
};
new RegExp(RegExp.escape(string));
var str = "aVal,bVal,cVal,dVal,eVal";
var rgx = 'bVal';
var x = 'replacement';
var res = str.replace(rgx, x);
console.log(res);
Try this
var targetValue = 'bVal';
var replaceValue = 'yourValue';
str = str.replace(targetValue , replaceValue);

How to get particular string from one big string ?

I have string
var str = "Ahora MXN$1,709.05" and wanted to get only
"MXN$1,709.05" from this.
Can someone please help me?
You can use substring or replace. With replace you are going to replace something with nothing.
replace
var str = 'Ahora MXN$1,709.05';
var sub = 'Ahora ';
var res = str.replace(sub,'');
substring
var str = 'Ahora MXN$1,709.05';
var sub = 'Ahora ';
var res = str.substring(sub.length);
JsFiddle
You can use either substring or Regex
Using substring
var str = "Ahora MXN$1,709.05";
var result = str.substring('Ahora '.length);
console.log(result);
Using Regex
var str = "Ahora MXN$1,709.05";
var myRegexp = /Ahora\s(.*?)(?:\s|$)/g;
var match = myRegexp.exec(str);
console.log(match[1]);

find first number in a string followed by x amount of characters

string sChar = "_$$$ASDF 123-456-789123123XXX";
string sChar = "$$VIC123-456-789pppEEX";
I would like to parse the above examples of sChar to result in the following value
123-456-789
What this regex would do is find the first Number in the string as well as the next 10 characters. The next 10 characters can be special characters, alpha, or numberic.
Here the solution for you:
var sChar = "_$$$ASDF 123-456-789123123XXX";
//string sChar = "$$VIC123-456-789pppEEX";
var indexDigit = sChar.search(/[\d]/);
var str = sChar.substring(indexDigit, indexDigit+11);
alert(str);
I see an answer like this:
var str = sChar.match(/\d.{10}/);
alert(str)
That won't work:
Try the following:
var sChar = "_$$$ASDF 123-4$6-7";
var sChar2 = "$$VIC987-6$4-3";
var indexDigit = sChar.search(/[\d]/);
var str = sChar.substring(indexDigit, indexDigit+11);
alert(str);//returns "123-4$6-7"
var str2 = sChar2.match(/\d.{10}/);
alert(str2);//returns null

Want to get specific value from string

I have a JavaScript string sentrptg2c#appqueue#sentrptg2c#vwemployees#.
I want to get last string vwemployees through RegExp or from any JavaScript function.
Please suggest a way to do this in JavaScript.
You can use the split function:
var str = "sentrptg2c#appqueue#sentrptg2c#vwemployees#";
str = str.split("#");
str = str[str.length-2];
alert(str);
// Output: vwemployees
The reason for -2 is because of the trailing #. If there was no trailing #, it would be -1.
Here's a JSFiddle.
var s = "...#value#";
var re = /#([^#]+)#^/;
var answer = re.match(s)[1] || null;
if you're sure the string will be separated by "#" then you can split on # and take the last entry... I'm stripping off the last #, if it's there, before splitting the string.
var initialString = "sentrptg2c#appqueue#sentrptg2c#vwemployees#"
var parts = initialString.replace(/\#$/,"").split("#"); //this produces an array
if(parts.length > 0){
var result = parts[parts.length-1];
}
Try something like this:
String.prototype.between = function(prefix, suffix) {
s = this;
var i = s.indexOf(prefix);
if (i >= 0) {
s = s.substring(i + prefix.length);
}
else {
return '';
}
if (suffix) {
i = s.indexOf(suffix);
if (i >= 0) {
s = s.substring(0, i);
}
else {
return '';
}
}
return s;
}
No magic numbers:
var str = "sentrptg2c#appqueue#sentrptg2c#vwemployees#";
var ar = [];
ar = str.split('#');
ar.pop();
var o = ar.pop();
alert(o);
jsfiddle example

Categories