简体   繁体   English

我需要一个正则表达式来匹配字符串的结尾

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

I need to remove everything after a specific pattern ("category: ") from a sting. 我需要从st中删除特定模式(“类别:”)之后的所有内容。 I've tried a few things including this, but can;t get it to work: 我已经尝试了一些方法,包括此方法,但无法使其正常工作:

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

and this 和这个

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

Any suggestions? 有什么建议么?

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

A String is not a RegExp in JavaScript. 字符串不是JavaScript中的RegExp。 Do this: 做这个:

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

尝试这个:

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

If you are only concerned about a fixed string category , you can use .indexOf and .substr. 如果只关心固定的字符串类别 ,则可以使用.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