I have string where I want to remove any letters and hyphens. I have a code like below,
var s = '-9d 4h 3m',
t = '1-22';
var p = /[^0-9-]+/g;
r = s.replace(p, ''),
a = t.replace(p, '');
console.log(r, a);
Here I want to remove hyphen if it is in between the numbers and omit at first. Any help or suggestions?
Fiddle
Much more simpler one without using | operator.
string.replace(/(?!^-)\D/g, "")
DEMO
You can use the following regex:
var p = /[^0-9-]+|(?:(?!^)-)/g;
See Fiddle
In your console log you put a comma between the variable but you need a plus like this.
I have also change variable a so that it removes the -
var s = '-9d 4h 3m';
var t = '1-22';
var p = /[^0-9-]+/g;
var r = s.replace(p, '');
var a = t.replace("-", '');
console.log(r + " " + a);
https://stackoverflow.com/a/1862219/3464552 check over here this will be a solution.
var s = '-9d 4h 3m',
s = s.replace(/\D/g,'');
Related
12:00:00:12
How to remove 6 character from the back? the output would be 12:00, I can't use substring to get the from the front to get the 6 char, because it can be 9:00 so it's just 4 char instead of 5.
I think #ZakariaAcharki is a better solution but if you want make it by substring try this:
str = '12:00:00:12';
str.substring(0,str.length-6);
I think better if you use split() function, and take the first and second items in splited array.
var my_string ="12:00:00:12";
var array_splited = my_string.split(':');
console.log( array_splited[0] + ':' + array_splited[1] ); //12:00
If you want it in single line, e.g :
my_string.split(':')[0] + ':' + my_string.split(':')[1];
Hope this helps.
You can determine the length and than go back 6 chars e.g.
str = '12:00:00:12'
str = str.substring(0,str.length - 6);
But you may better match with
str = '12:00:00:12'.match(/^[0-9]+:[0-9]+/)[0]
A regular expression with .match() method will do:
var str1 = '12:00:00:12';
var str2 = '9:40:00:12';
var regex = /(\d+)+:+(\d\d)/g;
var newStr1 = str1.match(regex)[0];
var newStr2 = str2.match(regex)[0];
document.querySelector('#one').textContent = JSON.stringify(newStr1);
document.querySelector('#two').textContent = JSON.stringify(newStr2);
'12:00:00:12' <pre id='one'></pre>
<hr>
'9:40:00:12' <pre id='two'></pre>
var str = "12:00:00:12";
var newStrArr = str.split(":");
newStrArr.pop();
newStrArr.pop();
newStrArr.join(":");
If the time will always be in the form (0-12):(00-59);(00-59) then you could use regex and the function .match() to get the time in the format you would like:
current_time = '12:00:00'
time_formatted = current_time.match(/\d+:\d+/)
Try using split and join.
EG 1:
var num = "12:00:00:12";
console.log(num.split(':', 2).join(':'));
EG 2:
var num = "9:00:00:12";
console.log(num.split(':', 2).join(':'));
Simple and best solution:
Use slice() function.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
$(function(){
var str = '12:00:00:12';
alert(str.slice(0,-6));
});
Output: 12:00
JSFiddle Demo
I have two requirements, first, I want to replace the "-" symbol in both beginning and end of the text with an empty value. Second, if there are any continuous "-" symbols they should be replaced with a single "-" symbol.
If possible please provide the code for both the requirements in a single pattern.
CODE:
//1.)
// replace more than 1 "-" in b
// Expected Output : -asdas-sadf-asdasd-ju
var a = "--asdas-sadf----asdasd---ju";
a = a.replace(/-{2,}/,"");
//alert(a);
//2.)
// remove last "-" and starting "-" from b that is "das-" - after das needs to be removed
// Expected output : welcome/asasdgrd/asd-ast-yret-das/456
var b = "-welcome/asasdgrd/asd-ast-yret-das-/456"
b = b.replace(/[-$]/,"");
//alert(b);
Fiddler Link:
http://jsfiddle.net/nj5j0yeq/1/
You need to use capturing groups.
var s = "--asdas-sadf----asdasd---ju";
alert(s.replace(/^-+|-+$|(-)+/gm, "$1"));
^-+|-(?!.*?-)|(-){2,}
You can try this.Replace by $1.See demo.
https://regex101.com/r/jV9oV2/8
You can check even time of -- and can replace with odd -
var a = "--asdas-sadf----asdasd---ju";
var b= a.split("--").join("-");
var c = b;
var d = c.split("--").join("-");
console.log(d);
or `var res = str.split(/^-+$|(-)+/).join("");
console.log(res);
`
I was wondering if there is a safe way (if the data is coming from users) to get the string and the number separated - for example "something-55", "something-124", "something-1291293"
I would want:
something and
55
something and
124
something and
1291293
I mean by a 'safe way' is to be certain I am getting only the number on the end.. if the data is coming from the users "something" could be anything some-thing-55 for example..
I'm looking for a robust way.
try this, working.
var string = 'something-456';
var array = string.split('-');
for (var i = 0;i<array.length;i++){
var number = parseFloat(array[i]);
if(!isNaN(number)){
var myNumber = number;
var mySomething = array[i - 1];
console.log('myNumber= ' + myNumber);
console.log('mySomething= ' + mySomething);
}
}
Can you try this?
var input='whatever-you-want-to-parse-324';
var sections=input.split(/[\w]+-/);
alert(sections[sections.length-1]);
You can use substr along with lastIndexOf:
var str = "something-somethingelse-55",
text = str.substr(0, str.lastIndexOf('-')),
number = str.substr(str.lastIndexOf('-') + 1);
console.log(text + " and " + number);
Fiddle Demo
All though it's a tad late, this would be the most restrictive solution:
var regex = /^([-\w])+?-(\d+)$/,
text = "foo-123",
match = test.match(regex);
You will get a match object back with the following values:
[ "foo-123", "foo", "123" ]
It's a very strict match so that " foo-123" and "foo-123 " would not match, and it requires the string to end in one or more digits.
I nee to replace all numbers after underline inside a string.
I think that I can use Regex, but I don't know how to use Regex Syntax
See an example of my string:
milton_0
milton_1
If that is the standard format, You can use split()
var str = 'milton_1';
alert(str.split('_')[1]);
You don't need regex for this. The following code in enough
var str = "milton_0";
str = str.substring(0,str.indexOf("_"));
I'm not sure how specific or broad you want to be, but you can try this:
var starter = "milton_1";
var specialVal = "asdf";
var re = /^(milton_)(\d+)$/;
var replaced = starter.replace(re, function (match, p1) {
return p1 + specialVal;
});
console.log(replaced);
http://jsfiddle.net/ne4cD/
This will match a string starting with "milton_" and ending with digits. It replaces any digits after the "_" with the specialVal value.
An example of simply incrementing that number is:
var starter = "milton_1";
var re = /^(milton_)(\d+)$/;
var replaced = starter.replace(re, function (match, p1, p2) {
return p1 + (+p2 + 1);
});
console.log(replaced);
http://jsfiddle.net/ne4cD/2/
UPDATE:
If the "milton" part isn't static, then you're really only targeting the "_" with digits after it. So something like this:
var starter = "asdfkjlasdfjksadf_1";
var specialVal = "asdf";
var re = /(_)(\d+)/g;
var replaced = starter.replace(re, function (match, p1) {
return p1 + specialVal;
});
console.log(replaced);
http://jsfiddle.net/ne4cD/3/
And maybe a little better to see: http://jsfiddle.net/ne4cD/4/
First of all you need them as list for handling easily.
var listOfStrings = yourStringObject('whateverYourCharacterUnderEachWord').ToList<string>();
After that you need to get rid of number for each string in the list and add what you want.
foreach(string word in listOfStrings){
word = word.Substring(0,word.IndexOf('_')+1);
word = word + "characterThatYouWantToAddHere"
}
This is my code :
var myStr = "/private_images/last-edit/image-work-med.png";
and I'd like to replace the last 7 chars (med.png) with big.png. Or, as you can see, the last occurence after a - split.
How can I do it? I think about regex, but I'm not a champion with them. Tried :
myStr = myStr .replace(/-([^-]*)$/, "big" + '$1');
but it replace the last -, not the last occurence. So the result is /private_images/last-edit/image-workbigmed.png
I'll make a confession: I'm not so great with regexes either.
How about splitting up using split? Less concise, but easier to understand.
var myStr = "/private_images/last-edit/image-work-med.png";
var strs = myStr.split('-');
// Change the last element.
strs[strs.length - 1] = "big.png";
// And put back the right string.
myStr = strs.join('-');
You could use a regex, or you could use a few string methods and make your intentions clear.
var idx = myStr.lastIndexOf("-");
var newStr = myStr.substring(0, idx) + "big.png";
Without using RegExp you could use:
var str = "/private_images/last-edit/image-work-med.png"
,replace = 'big.png'
,nwstr = str.slice(0,str.lastIndexOf('-')+1)+replace;
//=> nwstr now "/private_images/last-edit/image-work-big.png"
More 'functional':
var nwstr = function(s){
return s.replace(s.substr(-7),'');}(
'/private_images/last-edit/image-work-med.png'
)+'big.png'
var url = "/private_images/last-edit/image-work-med.png";
var index = url.lastIndexOf('-');
url = url.substring(0, index+1);
var url2 = "big.png";
var output = url.concat(url2); alert(output);
Check this
Just add '-' to your regex and to the replacement string:
myStr = myStr .replace(/-([^-]*)\.png$/, "-big.png");
Or if you want the file extension to be variable:
myStr = myStr .replace(/-([^-]*)\.([a-z]+)$/, "-big.$2");
Why not just use replace:
var myStr = "/private_images/last-edit/image-work-med.png";
var newStr = myStr.replace("med.png", "big.png");
According to the requirements specified in your question this would suffice.
If you know it will be a .png file:
var ex = new Regex(#"-\w*.png$");
var myStr = "/private_images/last-edit/image-work-med.png";
myStr = ex.Replace(myStr, "-big.png");
It works but if its a jpg it wont...
If you want to use string functions -
var myStr = "/private_images/last-edit/image-work-med.png";
var cleanedupStr = myStr.slice(0, myStr.lastIndexOf("-"));
String.slice