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()
thansplit()
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
1 Answer
Reset to default 5If 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
版权声明:本文标题:jquery - javascript split() string after Word that starts with a sequence of characterS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745220444a2648370.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论