1

My page looks like this

<head>
<title>story : cat</title>

Sometimes the title also looks like story | cat. This title is getting captured inside an anchor tag.

<a class="results" heref="www.hello.com" title="story : cat"/> 
<a class="results" heref="www.hello.com" title="story | cat"/>

How can I truncate the title value inside the anchor tag using javascript only to capture just the story-

<a class="results" heref="www.hello.com" title="story"/>
3
  • 2
    "This title is getting captured inside an anchor tag." How? Commented Dec 4, 2015 at 3:50
  • So, <title> or <a title="…">, which do you mean now? Commented Dec 4, 2015 at 3:52
  • If your title always has "story" word as the first word and has a "space" after it, you may like to split the title by "space", and then return the first index. Commented Dec 4, 2015 at 3:54

6 Answers 6

2

Split on the separator (use a simple regexp to split on either : or |) then pick the first part (and trim() it to remove extra spaces), assuming there is only ever two parts, and if there is more, that only the first is of interest:

"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'

Also works on multi-word first parts and with punctuation other than : and |:

"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() //=> '物語'

Here's a few examples with potentially problematic inputs, for evaluating the effectiveness of this answer.

If the title is empty or does not contain : or |:

"".split(/[:|]/)[0].trim() //=> ''
"foo bar".split(/[:|]/)[0].trim() => 'foo bar'

If the title contains only (any number of) ::

":".split(/[:|]/)[0].trim() //=> ''
":::".split(/[:|]/)[0].trim() //=> ''
"|".split(/[:|]/)[0].trim() //=> ''
"|||||".split(/[:|]/)[0].trim() //=> ''
" : : : ".split(/[:|]/)[0].trim() //=> ''
" : | : | : ".split(/[:|]/)[0].trim() //=> ''
":|:|:".split(/[:|]/)[0].trim() //=> ''

Comparing to other answers:

substring(0, 5)

"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"
":" -> ":"
":::" -> ":::"
"|" -> "|"
"|||||" -> "|||||"
" : : : " -> " : : "
" : | : | : " -> " : | "
":|:|:" -> ":|:|:"

/^\w+/ using .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"
":" -> ""
":::" -> ""
"|" -> ""
"|||||" -> ""
" : : : " -> ""
" : | : | : " -> ""
":|:|:" -> ""

/^\w+/ using .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"
":" -> ":"
":::" -> ":::"
"|" -> "|"
"|||||" -> "|||||"
" : : : " -> " : : : "
" : | : | : " -> " : | : | : "
":|:|:" -> ":|:|:"

substring(.indexOf(':') + 1)

"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"
":" -> ""
":::" -> "::"
"|" -> "|"
"|||||" -> "|||||"
" : : : " -> " : : "
" : | : | : " -> " | : | : "
":|:|:" -> "|:|:"

split and .pop()

Functionally equivalent to mine, except without the .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"
":" -> ""
":::" -> ""
"|" -> ""
"|||||" -> ""
" : : : " -> " "
" : | : | : " -> " "
":|:|:" -> ""
Sign up to request clarification or add additional context in comments.

Comments

1

Based on the answer in this post How to remove part of a string before a ":" in javascript?

You can use one of these methods easily:

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();

This solution is more scalable because you don't hardcode the index in which the Colon or the | character is located. You use functions to locate it, so you can later change the keyword and it will still work.

Comments

1

You may try to use the substring method. Pass your title into a var then substring it.

var str = "story : cat";

var res = str.substring(0, 5);

Comments

1

You can get the title of the page using:

document.title;

You can get the first word of the title using:

var titleFirstWord = document.title.replace(/(^\w+).*/,'$1');

I don't know how you want to get a reference to the link, but once you've done that, you can use:

linkRef.title = titleFirstWord.

Here's an example. Hover over the link to see what the title has been set to.

  
    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>  
  

Comments

1

Try using String.prototype.match() with RegExp /^\w+/ to match 0 or more word characters at beginning of document.title ; also substituting href for heref at a element

document.querySelector(".results")
.setAttribute("title", document.title.match(/^\w+/))
<head>
<title>story : cat</title>
  </head>
<a class="results" href="www.hello.com" title="">title</a>

Comments

1

You can do like this:

var results = documet.getElementsByTagName("results")[0].getAttribute("title");
var str = results.substring(0,5);
var str2 = results.slice(0,5);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.