簡體   English   中英

我需要一個正則表達式來匹配字符串的結尾

[英]I need a Regex to match to the end of string

我需要從st中刪除特定模式(“類別:”)之后的所有內容。 我已經嘗試了一些方法,包括此方法,但無法使其正常工作:

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

和這個

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

有什么建議么?

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

字符串不是JavaScript中的RegExp。 做這個:

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

嘗試這個:

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

如果只關心固定的字符串類別 ,則可以使用.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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM