Reputation: 124
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?
Upvotes: 0
Views: 50
Reputation: 9782
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);
Upvotes: 0
Reputation: 10627
A String is not a RegExp in JavaScript. Do this:
var text = text.replace(/(.*category\:).*$/, '$1');
Upvotes: 1