Skip to main content

grepString (function)​

Splits the string passed into it on \n and then returns the lines matching the specified pattern, as an array of strings or detail objects.

  • @param str — The string to search through.
  • @param pattern — The pattern to find. Can be a string or a RegExp.
  • @param options — Options which control matching behavior.

See also grepFile, grepArray, String.prototype.grep, and Array.prototype.grep.

Example

const lines = grepString("apple\nbanana\ncherry", /an/);
console.log(lines); // ["banana"]
const grepString: {
(
str: string,
pattern: string | RegExp,
options: GrepOptions & {
details: true;
},
): Array<GrepMatchDetail>;
(str: string, pattern: string | RegExp, options?: GrepOptions): Array<string>;
};

grepString(...) (call signature)​

(str: string, pattern: string | RegExp, options: GrepOptions & {
details: true;
}): Array<GrepMatchDetail>;

grepString(...) (call signature)​

(str: string, pattern: string | RegExp, options?: GrepOptions): Array<string>;

grepArray (function)​

Returns those Array items matching the specified pattern, as either an Array of items or an Array of detail objects.

  • @param targetArray — The Array of strings to search through. Non-strings will be coerced to string during matching.
  • @param pattern — The pattern to find. Can be a string or a RegExp.
  • @param options — Options which control matching behavior.

See also grepString, grepFile, String.prototype.grep, and Array.prototype.grep.

Example

const matches = grepArray(["apple", "banana", "cherry"], "an");
console.log(matches); // ["banana"]
const grepArray: {
<T>(
targetArray: Array<T>,
pattern: string | RegExp,
options: GrepOptions & {
details: true;
},
): Array<GrepMatchDetail<T>>;
<T>(
targetArray: Array<T>,
pattern: string | RegExp,
options?: GrepOptions,
): Array<T>;
};

grepArray(...) (call signature)​

<T>(targetArray: Array<T>, pattern: string | RegExp, options: GrepOptions & {
details: true;
}): Array<GrepMatchDetail<T>>;

grepArray(...) (call signature)​

<T>(targetArray: Array<T>, pattern: string | RegExp, options?: GrepOptions): Array<T>;

grepFile (function)​

Reads the file content at path, splits it on \n, and then returns the lines matching the specified pattern, as an array of strings or detail objects.

  • @param path — The path to the file to search through.
  • @param pattern — The pattern to find. Can be a string or a RegExp.
  • @param options — Options which control matching behavior.

See also grepArray, grepString, String.prototype.grep, and Array.prototype.grep.

Example

const todos = grepFile("README.md", /TODO/);
console.log(todos);
const grepFile: {
(
path: string | Path,
pattern: string | RegExp,
options: GrepOptions & {
details: true;
},
): Array<GrepMatchDetail>;
(
path: string | Path,
pattern: string | RegExp,
options?: GrepOptions,
): Array<string>;
};

grepFile(...) (call signature)​

(path: string | Path, pattern: string | RegExp, options: GrepOptions & {
details: true;
}): Array<GrepMatchDetail>;

grepFile(...) (call signature)​

(path: string | Path, pattern: string | RegExp, options?: GrepOptions): Array<string>;

String (interface)​

interface String {
grep: {
(
pattern: string | RegExp,
options: GrepOptions & {
details: true;
},
): Array<GrepMatchDetail>;
(pattern: string | RegExp, options?: GrepOptions): Array<string>;
};
}

String.grep (function property)​

Splits the target string on \n and then returns the lines matching the specified pattern, as an array of strings or detail objects.

  • @param str — The string to search through.
  • @param pattern — The pattern to find. Can be a string or a RegExp.
  • @param options — Options which control matching behavior.

See also grepString, grepArray, grepFile, and Array.prototype.grep.

Example

const lines = "apple\nbanana\ncherry".grep(/an/);
console.log(lines); // ["banana"]
grep: {
(pattern: string | RegExp, options: GrepOptions & {
details: true;
}): Array<GrepMatchDetail>;
(pattern: string | RegExp, options?: GrepOptions): Array<string>;
};

String.grep(...) (call signature)​

(pattern: string | RegExp, options: GrepOptions & {
details: true;
}): Array<GrepMatchDetail>;

String.grep(...) (call signature)​

(pattern: string | RegExp, options?: GrepOptions): Array<string>;

Array (interface)​

interface Array<T> {
grep: {
(
pattern: string | RegExp,
options: GrepOptions & {
details: true;
},
): Array<GrepMatchDetail<T>>;
(pattern: string | RegExp, options?: GrepOptions): Array<T>;
};
}

Array.grep (function property)​

Returns those Array items matching the specified pattern, as either an Array of items or an Array of detail objects.

  • @param pattern — The pattern to find. Can be a string or a RegExp.
  • @param options — Options which control matching behavior.

See also grepString, grepArray, grepFile, and String.prototype.grep.

Example

const matches = ["apple", "banana", "cherry"].grep(/an/);
console.log(matches); // ["banana"]
grep: {
(pattern: string | RegExp, options: GrepOptions & {
details: true;
}): Array<GrepMatchDetail<T>>;
(pattern: string | RegExp, options?: GrepOptions): Array<T>;
};

Array.grep(...) (call signature)​

(pattern: string | RegExp, options: GrepOptions & {
details: true;
}): Array<GrepMatchDetail<T>>;

Array.grep(...) (call signature)​

(pattern: string | RegExp, options?: GrepOptions): Array<T>;

GrepOptions (interface)​

declare interface GrepOptions {
inverse?: boolean;
details?: boolean;
}

GrepOptions.inverse (boolean property)​

When inverse is true, the grep function returns those lines which DON'T match the pattern, instead of those which do. Defaults to false.

Example

const fruits = "apple\nbanana\ncherry";
const nonMatching = grepString(fruits, /an/, { inverse: true });
console.log(nonMatching); // ["apple", "cherry"]
inverse?: boolean;

GrepOptions.details (boolean property)​

When details is true, the grep function returns an array of GrepMatchDetail objects instead of an array of strings. Defaults to false.

Example

const fruits = "apple\nbanana\ncherry";
const details = grepString(fruits, /an/, { details: true });
console.log(details[0].lineNumber, details[0].lineContent);
details?: boolean;

GrepMatchDetail (interface)​

When grepString, grepArray, grepFile, or String.prototype.grep are called with the { details: true } option set, an Array of GrepMatchDetail objects is returned.

Example

const [detail] = grepString("apple\nbanana", /an/, { details: true });
console.log(detail.lineNumber, detail.lineContent, detail.matches);
declare interface GrepMatchDetail<ItemType = string> {
lineNumber: number;
lineContent: ItemType;
matches: RegExpMatchArray;
index: number;
content: ItemType;
}

GrepMatchDetail.lineNumber (number property)​

lineNumber: number;

GrepMatchDetail.lineContent (ItemType property)​

lineContent: ItemType;

GrepMatchDetail.matches (RegExpMatchArray property)​

matches: RegExpMatchArray;

GrepMatchDetail.index (number property)​

Same as lineNumber - 1.

Example

const [detail] = grepString("apple\nbanana", /an/, { details: true });
console.log(detail.index === detail.lineNumber - 1);
index: number;

GrepMatchDetail.content (ItemType property)​

Alias for lineContent.

Example

const [detail] = grepString("apple\nbanana", /an/, { details: true });
console.log(detail.content === detail.lineContent);
content: ItemType;