0

I want to match below strings for the prefix.

customer

client

agent

If my string contains any of the above, i want to ignore.

let str = "customer_service"

if(!str.startsWith("customer") && !str.startsWith("client") && !str.startsWith("agent"))
    return true;

Any easy and effective solution available? Thanks in advance.

2
  • Do you mean let str = 'customer_service'? WIth your code, str is simply assigned the value of a variable called customer_service Commented Apr 9, 2020 at 17:15
  • you want to discard customer, client and agent right? Commented Apr 9, 2020 at 17:16

2 Answers 2

2

You can test the string against a regular expression. If not match then proceed and ignore otherwise.

const regex = /^(customer|client|agent).*/;
const str = "customer_service";

if (!regex.test(str)) {
  // Ignored.
  // Do whatever you want to do
}
Sign up to request clarification or add additional context in comments.

4 Comments

Amazing! Worked like a charm! Thank you so much.
You are welcome.
Actually best idea :)
Thank you @GrégoryNEUT
0

You could use a regular expression to look for words or patterns:

const strings = [
  "customer_service",
  "wookies",
  "bananas",
  "hats",
  "guys named steve",
  "agent_request",
  "client_lunch_order",
];

// test pattern for the given words
const regex = m = /(customer|agent|client)/;

// test the given string against the pattern
const filterFn = str => !m.test(str)

// run the list through the filter
const filteredList = strings.filter(filterFn);

// display the filtered list
console.log(filteredList);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.