# words

### 说明

&#x20;按照指定模式, 拆分字符串 `string` 中的词为数组

### 源码

<https://github.com/ddzy/ts-utility-plugins/tree/master/src/ddzy/utility/string/words>

### 用法

{% code title="words.test.ts" %}

```typescript
import { words } from "../../utility/string/words";

describe('Method words() test: ', () => {
  test('words() should use the default pattern while receive none pattern', () => {
    const received = {
      str: 'duan zhao _*$*$  y & ang a p     g',
      reg: /\W+/,
    };
    const expected = ["duan", "zhao _*$*$  y & ang a p     g"];

    const result = words(received.str, received.reg);

    result.forEach((v, i) => {
      expect(v).toBe(expected[i]);
    });
  });

  test('words() can use custom pattern', () => {
    const received = [
      {
        str: 'duan  zhao    yang',
        reg: /\s+/g,
      },
      {
        str: 'duan ### zhaoyang |%# s d h   dan',
        reg: /#+/g,
      },
    ];
    const expected = [
      ["duan", "zhao", "yang"],
      ["duan ", " zhaoyang ", "%", " s d h   dan"],
    ];

    received.forEach((outerV, outerI) => {
      const result = words(outerV.str, outerV.reg);

      result.forEach((innerV, innerI) => {
        expect(innerV).toBe(expected[outerI][innerI]);
      });
    });
  });
});
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ddzy.gitbook.io/ts-utility-plugins-docs/utility/utility-string/words.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
