2016-06-15 102 views
0

我需要从一个特定的模式(“类别:”)后删除所有东西。我已经尝试了一些事情,包括这一点,但可以;吨得到它的工作:我需要一个正则表达式来匹配字符串的末尾

text = text.replace("category:/([^/]*)$", ""); 

text = text.replace("category: \w+", ""); 

有什么建议?

+2

你需要转义'/'s,但目前还不清楚为什么你要使用这些。请张贴一些样本的输入和所需的输出。 –

+0

尝试'类别:[\ s \ S] *' –

+0

您可以在问题中包含示例字符串和预期结果吗? – guest271314

回答

1
text = text.replace(/category:.*/, ""); 
+0

谢谢,我用你的建议,它工作得很好。 –

0

试试这个:

text.replace(/category:.*/, 'category:') 
1

的字符串不是JavaScript的一个RegExp。这样做:

var text = text.replace(/(.*category\:).*$/, '$1'); 
0

如果你只关心一个固定的字符串类别,您可以使用.indexOf和.substr。

var testString = 'category: stuff-to-be-removed'; 
var startPoint, endPoint, truncatedString; 

startPoint = testString.indexOf('category:'); 
endPoint = 'category:'.length; 
truncatedString = testString.substr(startPoint,endPoint); 
console.log(truncatedString); 
相关问题