admin管理员组

文章数量:1415145

This seems like it should be a very easy thing to do but I cannot seem to find it anywhere.

How can I use Javascript split() to split the string after a certain word that starts with say abcd.... so if I have "abcdHello one two three" , I would get " one two three". I am assuming that the abcd.... word will be at the beginning of the string. Thanks!

var allClassesString = $('.'+ui.item.overRow).find('.span12').attr('class');
var truncClassesString = allClassesString.split('span^'); // or span* - neither one works.

This seems like it should be a very easy thing to do but I cannot seem to find it anywhere.

How can I use Javascript split() to split the string after a certain word that starts with say abcd.... so if I have "abcdHello one two three" , I would get " one two three". I am assuming that the abcd.... word will be at the beginning of the string. Thanks!

var allClassesString = $('.'+ui.item.overRow).find('.span12').attr('class');
var truncClassesString = allClassesString.split('span^'); // or span* - neither one works.
Share Improve this question edited Jun 12, 2013 at 14:42 Georgi Angelov asked Jun 12, 2013 at 14:32 Georgi AngelovGeorgi Angelov 4,38812 gold badges70 silver badges100 bronze badges 8
  • 5 jQuery has no split function. But JavaScript does. Please post the code you've tried. – j08691 Commented Jun 12, 2013 at 14:33
  • 3 Thanks. Fixed it. People seem to be on fire for an honest mistake. – Georgi Angelov Commented Jun 12, 2013 at 14:35
  • @GeorgiAngelov Feel for you man--ignore the rude ones. – landons Commented Jun 12, 2013 at 14:36
  • 3 This seems to be more of a replace() than split() use-case. – David Thomas Commented Jun 12, 2013 at 14:37
  • Not really @DavidThomas . I was really interested in simply truncating my string by removing the abcd... I guess I could have replaced the abcd... word with "" but what Dogbert suggested first, worked perfectly. – Georgi Angelov Commented Jun 12, 2013 at 14:45
 |  Show 3 more ments

1 Answer 1

Reset to default 5

If I understood correctly, you want this:

"abcdHello one two three".split(/^abcd\S*/)
=> ["", " one two three"]

RegExp explanation:

^    -> Start of string
abcd -> Match "abcd" literally
\S   -> Not a whitespace
*    -> Repeat "\S" as many times as possible.

After your ment on the question, if you just want to remove the matching text, use .replace as @DavidThomas suggested.

"abcdHello one two three".replace(/^abcd\S*/, '')
=> " one two three"

Add g modifier to the RegExp if you want to replace more than one occurance:

"abcdHello one two three".replace(/^abcd\S*/g, '')
" one two three"

本文标签: jqueryjavascript split() string after Word that starts with a sequence of characterSStack Overflow