Slim
Slim

Reputation: 6187

How to replace a string using regexp in JavaScript

I want to filter a given string and replace some common words from a given string:

const str ='api/knwl/tests/products';
const str1 = 'api/users';
const str2 = 'api/tests/providers';

I want to filter the words 'api', 'knwl' or 'tests' from a given strings.

I tried somthing with regexp:

/(?!(tests|api|knwl))/g 

But it doesn't work. How can I fix this issue? I'm not expert on regexp.

Upvotes: 1

Views: 48

Answers (1)

front_end_dev
front_end_dev

Reputation: 2056

Regex - /(tests|api|knwl)/g;

const str ='api/knwl/tests/products';
const str1 = 'api/users';
const str2 = 'api/tests/providers';

const filterRegex = str.replace(/(tests|api|knwl)/g,''); // replace all occurence of strings (tests,api,knwl) to empty.

Upvotes: 1

Related Questions