How can I delete the first word from a line?
Mon 25-Jul-2011
I want to delete the first word "Mon" with jav开发者_如何学Cascript jQuery. How can i do this ?
If you don't want to split the string (faster, less memory consumed), you can use indexOf() with substr():
var original = "Mon 25-Jul-2011";
var result = original.substr(original.indexOf(" ") + 1);
var string = "Mon 25-Jul-2011";
var parts = string.split(' ');
parts.shift(); // parts is modified to remove first word
var result;
if (parts instanceof Array) {
result = parts.join(' ');
}
else {
result = parts;
}
// result now contains all but the first word of the string.
I wanted to remove first word from each items in Array of strings. I did that using split
, slice
, join
.
var str = "Mon 25-Jul-2011"
var newStr = str.split(' ').slice(1).join(' ')
console.log(str)
Run this code in console you will get the expected string.
Another solution:
var line = "Mon 25-Jul-2011";
var edited = line.substring( line.indexOf(" ") + 1, line.length );
You can manipulate any dom, using their reference id, class or tag. Example
<div id="date">Mon 25-Jul-2011</div>
<script>
$(document).ready(function() {
var strDate = $('#date').html();
// Using regex, this will remove any day which may present in your date DOM
strDate.replace(/(mon|tue|wed|thu|fri|sat)/i, '');
// This to trim any space present
strDate.replace(/^\s+|\s+$/g,'');
$('#date').html(strDate);
});
</script>
var str = "Mon 25-Jul-2011";
var firstSpace=str.indexOf(" ");
var newStr= str.slice(firstSpace);
//result:"25-Jul-2011"
This should output "25-Jul-2011":
var string = "Mon 25-Jul-2011";
string = string.split(' ').pop();
精彩评论