如何使用javascript正则表达式,从html获取第一个单词和最后一个单词

时间:2011-11-09 16:31:21

标签: javascript regex

javascript如何从html中预先匹配第一个单词和最后一个单词?

例如,在下面的句子中,我想要获得单词Theunderway。感谢。

<p>The Stack Overflow 2011 community moderator election is underway</p>
//should consider the different html tags.

2 个答案:

答案 0 :(得分:4)

您不需要正则表达式:

var words = "The Stack Overflow 2011 community moderator election is underway".split(" "),
    first = words[0],
    last = words[words.length-1];

答案 1 :(得分:4)

如果元素已经在文档中,您可以获取其文本内容并根据" "进行拆分:

var text  = "textContent" in document.body ? "textContent" : "innerText",
    el    = document.getElementById("myElement"),
    arr   = el[text].split(" "),
    first = arr.shift(),
    last  = arr.pop();

alert("1st word is '"+first+"', last word is '"+last+"'.");

如果它还不是元素,请将其设为:

var arr, first, last,
    text  = "textContent" in document.body ? "textContent" : "innerText",
    html = "<p>The Stack Overflow 2011 community moderator election is underway</p>",
    el   = document.createElement("div");

el.innerHTML = html;
arr   = el[text].split(" "),
first = arr.shift(),
last  = arr.pop();

alert("1st word is '"+first+"', last word is '"+last+"'.");  

注意:这不会考虑标点符号 - 您可能希望在拆分之前使用简单的正则表达式从字符串中删除其中的一些。另外,如果文字中只有一个字词,last将为undefined。如果没有单词,1st将是一个空字符串"",最后一个仍然是undefined