John Knutson
John Knutson

Reputation: 124

I need a Regex to match to the end of string

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

Answers (4)

user2182349
user2182349

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

StackSlave
StackSlave

Reputation: 10627

A String is not a RegExp in JavaScript. Do this:

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

Upvotes: 1

chungtinhlakho
chungtinhlakho

Reputation: 930

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

Upvotes: 1

Mengo
Mengo

Reputation: 1267

Try this:

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

Upvotes: 0

Related Questions