假设我有一个这样的字符串:
"This belongs to that then there's this other keyword which does something else."
我将如何截断它,以便它找到keyword
并截断keyword which does something else.
只给我:
"This belongs to that then there's this other "
谢谢!
假设我有一个这样的字符串:
"This belongs to that then there's this other keyword which does something else."
我将如何截断它,以便它找到keyword
并截断keyword which does something else.
只给我:
"This belongs to that then there's this other "
谢谢!
1.9.3p327 > str = "This belongs to that then there's this other keyword which does something else."
=> "This belongs to that then there's this other keyword which does something else."
1.9.3p327 > str.sub(/keyword.*/, '')
=> "This belongs to that then there's this other "
不过要小心,因为这也会匹配“keywordwithnotrailingspace”。查看 \b 以获得单词边界...
添加到您的工具带的一个简单工具是Rubular。你可以在你的浏览器中测试各种很棒的正则表达式......教人钓鱼......你知道这句话。:)
@emm - 我认为@Philip 的回答是正确的;但是,我会结合使用这两个答案并选择keyword[.|\s]*