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"
":" -> ""
":::" -> ""
"|" -> ""
"|||||" -> ""
" : : : " -> " "
" : | : | : " -> " "
":|:|:" -> ""
<title>or<a title="…">, which do you mean now?