答案 0 :(得分:2)
在分隔符上拆分(使用简单的正则表达式拆分 :
或|
)然后选择第一部分(并trim()
将其删除额外的空间),假设只有两个部分,如果还有更多,那么只有第一部分是有意义的:
"story : cat".split(/[:|]/)[0].trim() //=> 'story'
"story | cat".split(/[:|]/)[0].trim() //=> 'story'
"dog : cat".split(/[:|]/)[0].trim() //=> 'dog'
"story : chapter : cat".split(/[:|]/)[0].trim() //=> 'story'
同样适用于多字第一部分以及:
和|
以外的标点符号:
"once upon a story : cat".split(/[:|]/)[0].trim() //=> 'once upon a story'
"hey! a story? : cat".split(/[:|]/)[0].trim() //=> 'hey! a story?'
"ß—¯±× : cat".split(/[:|]/)[0].trim() //=> 'ß—¯±×'
"物語 : 猫".split(/[:|]/)[0].trim() //=> '物語'
以下是一些可能存在问题的输入示例,用于评估此答案的有效性。
如果标题为空或不包含:
或|
:
"".split(/[:|]/)[0].trim() //=> ''
"foo bar".split(/[:|]/)[0].trim() => 'foo bar'
如果标题仅包含 (任意数量):
:
":".split(/[:|]/)[0].trim() //=> ''
":::".split(/[:|]/)[0].trim() //=> ''
"|".split(/[:|]/)[0].trim() //=> ''
"|||||".split(/[:|]/)[0].trim() //=> ''
" : : : ".split(/[:|]/)[0].trim() //=> ''
" : | : | : ".split(/[:|]/)[0].trim() //=> ''
":|:|:".split(/[:|]/)[0].trim() //=> ''
与其他答案相比:
"story : cat" -> "story"
"story | cat" -> "story"
"dog : cat" -> "dog :"
"story : chapter : cat" -> "story"
"once upon a story : cat" -> "once "
"hey! a story? : cat" -> "hey! "
"ß—¯±× : cat" -> "ß—¯±×"
"物語 : 猫" -> "物語 : "
"" -> ""
"foo bar" -> "foo b"
":" -> ":"
":::" -> ":::"
"|" -> "|"
"|||||" -> "|||||"
" : : : " -> " : : "
" : | : | : " -> " : | "
":|:|:" -> ":|:|:"
.match()
"story : cat" -> "story"
"story | cat" -> "story"
"dog : cat" -> "dog"
"story : chapter : cat" -> "story"
"once upon a story : cat" -> "once"
"hey! a story? : cat" -> "hey"
"ß—¯±× : cat" -> ""
"物語 : 猫" -> ""
"" -> ""
"foo bar" -> "foo"
":" -> ""
":::" -> ""
"|" -> ""
"|||||" -> ""
" : : : " -> ""
" : | : | : " -> ""
":|:|:" -> ""
.replace()
"story : cat" -> "story"
"story | cat" -> "story"
"dog : cat" -> "dog"
"story : chapter : cat" -> "story"
"once upon a story : cat" -> "once"
"hey! a story? : cat" -> "hey"
"ß—¯±× : cat" -> "ß—¯±× : cat"
"物語 : 猫" -> "物語 : 猫"
"" -> ""
"foo bar" -> "foo"
":" -> ":"
":::" -> ":::"
"|" -> "|"
"|||||" -> "|||||"
" : : : " -> " : : : "
" : | : | : " -> " : | : | : "
":|:|:" -> ":|:|:"
"story : cat" -> " cat"
"story | cat" -> "story | cat"
"dog : cat" -> " cat"
"story : chapter : cat" -> " chapter : cat"
"once upon a story : cat" -> " cat"
"hey! a story? : cat" -> " cat"
"ß—¯±× : cat" -> " cat"
"物語 : 猫" -> " 猫"
"" -> ""
"foo bar" -> "foo bar"
":" -> ""
":::" -> "::"
"|" -> "|"
"|||||" -> "|||||"
" : : : " -> " : : "
" : | : | : " -> " | : | : "
":|:|:" -> "|:|:"
功能上与我的相同,除了没有.trim()
。
"story : cat" -> " cat"
"story | cat" -> " cat"
"dog : cat" -> " cat"
"story : chapter : cat" -> " cat"
"once upon a story : cat" -> " cat"
"hey! a story? : cat" -> " cat"
"ß—¯±× : cat" -> " cat"
"物語 : 猫" -> " 猫"
"" -> ""
"foo bar" -> "foo bar"
":" -> ""
":::" -> ""
"|" -> ""
"|||||" -> ""
" : : : " -> " "
" : | : | : " -> " "
":|:|:" -> ""
答案 1 :(得分:1)
您可以尝试使用substring方法。 将你的标题传递给var然后将其子串。
var str =" story:cat&#34 ;;
var res = str.substring(0,5);
答案 2 :(得分:1)
你可以这样做:
var results = documet.getElementsByTagName("results")[0].getAttribute("title");
var str = results.substring(0,5);
var str2 = results.slice(0,5);
答案 3 :(得分:1)
根据这篇文章中的答案How to remove part of a string before a ":" in javascript?
您可以轻松使用其中一种方法:
var str = document.getElementsByClassName('results')[0].getAttribute("title"); //Use loop for other indexes
str = str.substring(str.indexOf(":") + 1);
OR
var str = document.getElementsByClassName('results')[0].getAttribute("title"); //Use loop for other indexes
str = str.split(":").pop();
此解决方案更具可扩展性,因为您不会对冒号或|的索引进行硬编码字符位于。您可以使用函数来定位它,以便稍后更改关键字,它仍然有效。
答案 4 :(得分:1)
您可以使用以下方式获取页面标题:
document.title;
您可以使用以下方式获取标题的第一个单词:
var titleFirstWord = document.title.replace(/(^\w+).*/,'$1');
我不知道您希望如何获得该链接的引用,但是一旦您完成了该链接,您就可以使用:
linkRef.title = titleFirstWord.
这是一个例子。将鼠标悬停在链接上可查看标题的设置。
var titleFirstWord = document.title.replace(/(^\w+).*/,'$1');
document.links[0].title = titleFirstWord;

<head>
<title>story : cat</title>
</head>
<a href="http://www.foo.com" title="whatever">whatever</a>
&#13;
答案 5 :(得分:1)
尝试使用messageLabel.delegate
与String.prototype.match()
RegExp
匹配/^\w+/
开头的0个或多个字词。也将document.title
替换为href
元素<{1}}
heref
&#13;
a
&#13;