Javascript 如何从一行中删除第一个单词?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6871403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I delete the first word from a line?
提问by prime
Mon 25-Jul-2011
I want to delete the first word "Mon" with javascript jQuery. How can i do this ?
我想用 javascript jQuery 删除第一个单词“Mon”。我怎样才能做到这一点 ?
回答by Frédéric Hamidi
回答by ChristopheCVB
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.
回答by Rahul Sagore
I wanted to remove first word from each items in Array of strings. I did that using split
, slice
, join
.
我想从字符串数组中的每个项目中删除第一个单词。我使用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.
在控制台中运行此代码,您将获得预期的字符串。
回答by toopay
You can manipulate any dom, using their reference id, class or tag. Example
您可以使用它们的引用 ID、类或标签来操作任何 dom。例子
<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>
回答by JWeary
Another solution:
另一种解决方案:
var line = "Mon 25-Jul-2011";
var edited = line.substring( line.indexOf(" ") + 1, line.length );
回答by foz
This should output "25-Jul-2011":
这应该输出“25-Jul-2011”:
var string = "Mon 25-Jul-2011";
string = string.split(' ').pop();
回答by Refael
var str = "Mon 25-Jul-2011";
var firstSpace=str.indexOf(" ");
var newStr= str.slice(firstSpace);
//result:"25-Jul-2011"