I need to remove everything after a specific pattern ("category: ") from a sting. 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. 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.
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.