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
Related
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);
How can i extract from string only last number which should be "5"?
var str = "1000040928423195 points added to your balance";
var str = parseInt(str);
var lastNum = str.substr(str.length - 1);
console.log(lastNum);
Given your string...
var str = "1000040928423195 points added to your balance";
... we can extract all the numbers with a regex...
var onlyNumbers = str.replace(/\D/g,'');
... and, finally, get the last one:
var lastNumber = onlyNumbers.substring(onlyNumbers.length - 1);
Here is a demo:
var str = "1000040928423195 points added to your balance";
var onlyNumbers = str.replace(/\D/g,'');
var lastNumber = onlyNumbers.substring(onlyNumbers.length - 1);
console.log(lastNumber);
Just add toString() after parseInt
var str = "1000040928423195 points added to your balance";
var str = parseInt(str).toString();
var lastNum = str.substr(str.length - 1);
console.log(lastNum);
Use a regex like /\d+(\d)/
var str = "1000040928423195 points added to your balance";
str.match(/\d+(\d)/)[1]
After your second line, str isn't a string any more; it is an integer. As such, you can extract the last digit with str % 10; if necessary, convert that to a string.
Try parsing it with a Regular Expression like so :)
string = "120394023954932503 fdsaf fdsaf dasfasd";
regex = /([0-9]*)/g;
match = regex.exec(string);
last_num = match[0].substr(match[0].length - 1);
console.log(last_num);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="string_val"></div>
Use the regex \d+(\d) to match multiple digits followed by a digit.
\d will occupy as many as possible, where (\d) will match the last.
Then get the last number from the first group [1].
var str = "1000040928423195 points added to your balance";
var match = /\d+(\d)/g.exec(str)[1];
console.log(match);
var digit = /.*(\d)/.exec("awoo123awoo")[1];
3
var str = '#/promotionalMailer/test1';
output should be ==> #/promotionalMailer
I want the string before the second slash '/'
I have tried this so far:
var str = '#/promotionalMailer/test1';
var match = str.match(/([^\/]*\/){2}/)[0];
alert(match);
But it comes with the second slash.
try split, slice and join
var str = '#/promotionalMailer/test1';
console.log( str.split("/").slice(0,2).join("/"));
For example,
var str = '#/promotionalMailer/test1/foo/bar/baz';
result = str.split('/').slice(0, 2).join('/')
document.write('<pre>'+JSON.stringify(result,0,3));
If you want regexes, then
var str = '#/promotionalMailer/test1/foo/bar/baz';
result = str.match(/[^\/]*\/[^\/]*/)[0]
document.write('<pre>'+JSON.stringify(result,0,3));
How can i correctly cut out letter "v" and alert str without "v"
var str = "Javascript";
var cut = str.substring(2,3);
var str = "Javascript";
var cut = str.substring(0,2) + str.substring(3);
alert(cut);
You're using the right tool (String#substring). You need two substrings that you put back together, the "Ja" and the "ascript". So:
var str = "Javascript";
var cut = str.substring(0, 2) + str.substring(3);
alert(cut);
Another option would be String#replace, which will replace the first occurrence of what you give it unless you tell it to do it globally with a regex and the g flag (which we won't, because we just want to remove that one v):
var str = "Javascript";
var cut = str.replace("v", "");
alert(cut);
Just for fun, there is another way, but it's a bit silly: You can split the string into an array of single-character strings, remove the third entry from the array, and then join it back together:
var str = "Javascript";
var cut = str.split("").filter(function(_, index) {
return index != 2;
}).join("");
alert(cut);
or
var str = "Javascript";
var cut = str.split("");
cut.splice(2, 1); // Delete 1 entry at index 2
cut = cut.join("");
alert(cut);
...but again, that's a bit silly. :-)
use replace method
var str = "Javascript";
str = str.replace("v", "");
alert(str);
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]);