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);