1

I have a list of string in node.js

var listStr = ["helloworld","helloworld1","helloworld2","somethingelse"]

I want to find all strings which meet some regex. For example, for the regex: *world*, I would get the next strings: "helloworld","helloworld1","helloworld2"

there is any npm package or function which get a list of strings and regex, and return the strings which meet the regex?

2 Answers 2

1

You don't need an NPM package for this:

var filtered = listStr.filter(function (item) {
    return item.match(/world/);
});

Contents of filtered:

[ 'hello world',
  'helloworld1',
  'helloworld2' ]
Sign up to request clarification or add additional context in comments.

Comments

0

You can do this in plain JavaScript. Just fix your regex, and use Array#filter():

listStr.filter(function(str){return /.*world.*/.test(str)})
                                     ^^     ^^ proper wildcard

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.