繁体   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