Skip to content

match v3.15.0+

Using match api to match Chinese text string with pinyin string.

Example

Basic Use

If Pinyin matches text, return the indexes of the matching text.

js
import { match } from 'pinyin-pro';

match('汉语拼音', 'hanyupinyin'); // [0, 1, 2, 3]
import { match } from 'pinyin-pro';

match('汉语拼音', 'hanyupinyin'); // [0, 1, 2, 3]

Continuous

Using the continuous attribute to specify whether the matching Chinese indexes are continuous is considered a successful match (the default value is false, which means continuous matching is not required):

js
import { match } from 'pinyin-pro';

match('汉语拼音', 'hanpin'); // [0, 2]

match('汉语拼音', 'hanpin', { continuous: true }); // null
import { match } from 'pinyin-pro';

match('汉语拼音', 'hanpin'); // [0, 2]

match('汉语拼音', 'hanpin', { continuous: true }); // null

Precision

The precision attribute can be used to control the accuracy of Chinese character and Pinyin matching:

js
import { match } from 'pinyin-pro';

// 默认首字母匹配算匹配成功
match('中文拼音', 'zwpy'); // [0, 1, 2, 3]

// every 需要每一个字符都匹配成功
match('中文拼音', 'zwpy', { precision: 'every' }); // null
match('中文拼音', 'zhongwenpinyin', { precision: 'every' }); // [0, 1, 2, 3]

// start 只要开头任意个字母匹配就算匹配成功
match('中文拼音', 'zhwpy', { precision: 'start' }); // [0, 1, 2, 3]
match('中文拼音', 'zhwpy'); // null

// any 任意有一个字母匹配就算匹配成功
match('中文拼音', 'ongwpy', { precision: 'any' }); // [0, 1, 2, 3]
match('中文拼音', 'ongwpy'); // null
import { match } from 'pinyin-pro';

// 默认首字母匹配算匹配成功
match('中文拼音', 'zwpy'); // [0, 1, 2, 3]

// every 需要每一个字符都匹配成功
match('中文拼音', 'zwpy', { precision: 'every' }); // null
match('中文拼音', 'zhongwenpinyin', { precision: 'every' }); // [0, 1, 2, 3]

// start 只要开头任意个字母匹配就算匹配成功
match('中文拼音', 'zhwpy', { precision: 'start' }); // [0, 1, 2, 3]
match('中文拼音', 'zhwpy'); // null

// any 任意有一个字母匹配就算匹配成功
match('中文拼音', 'ongwpy', { precision: 'any' }); // [0, 1, 2, 3]
match('中文拼音', 'ongwpy'); // null

Space

Use the space attribute to control whether the spaces don't participate in matching:

js
import { match } from 'pinyin-pro';

// 默认不参与匹配
match('汉语拼音', 'han yupinyin'); // [0, 1, 2, 3]

match('汉语拼音', 'han yupinyin', { space: 'preserve' }); // null
import { match } from 'pinyin-pro';

// 默认不参与匹配
match('汉语拼音', 'han yupinyin'); // [0, 1, 2, 3]

match('汉语拼音', 'han yupinyin', { space: 'preserve' }); // null

lastPrecision

The lastPrecision attribute can be used to control the accuracy of matching the last Chinese character with Pinyin. By default, when the precision is any the lastPrecision is any; Otherwise, lastPrecision is start'.

js
import { match } from 'pinyin-pro';

// default
match('汉语拼音', 'hanyupiny'); // [1, 2, 3, 4]

// specify lastPrecision
match('汉语拼音', 'hanyupiny', { lastPrecision: 'every' }); // null
import { match } from 'pinyin-pro';

// default
match('汉语拼音', 'hanyupiny'); // [1, 2, 3, 4]

// specify lastPrecision
match('汉语拼音', 'hanyupiny', { lastPrecision: 'every' }); // null

Polyphonic Matching

For polyphonic characters, as long as one of the Pinyins matches, the matching is considered successful:

js
import { match } from 'pinyin-pro';

match('会计', 'kuaiji'); // [0, 1]
match('会计', 'huiji'); // [0, 1]
import { match } from 'pinyin-pro';

match('会计', 'kuaiji'); // [0, 1]
match('会计', 'huiji'); // [0, 1]

v match ü v3.25.0+

The v attribute can be used to allow using v to match ü:

js
import { match } from 'pinyin-pro';

match('吕布', 'lvbu'); // [0, 1]
import { match } from 'pinyin-pro';

match('吕布', 'lvbu'); // [0, 1]

API

Function

js
import { match } from 'pinyin-pro';

interface MatchOptions {
  precision?: 'first' | 'start' | 'every' | 'any';
  continuous?: boolean;
  space?: 'ignore' | 'preserve';
  lastPrecision?: 'first' | 'start' | 'every' | 'any';
  v?: boolean; // v3.25.0+
}

function match(text: string, pinyin: stringoptions?: MatchOptions): number[] | null; // 匹配成功返回匹配汉字对应下标数组; 不成功返回 null
import { match } from 'pinyin-pro';

interface MatchOptions {
  precision?: 'first' | 'start' | 'every' | 'any';
  continuous?: boolean;
  space?: 'ignore' | 'preserve';
  lastPrecision?: 'first' | 'start' | 'every' | 'any';
  v?: boolean; // v3.25.0+
}

function match(text: string, pinyin: stringoptions?: MatchOptions): number[] | null; // 匹配成功返回匹配汉字对应下标数组; 不成功返回 null

Parameters

  • text (required):string, Chinese text to match
  • pinyin (required):string, Pinyin to match
  • options (optional): object, the configuration of matching rules, more specific information is shown in the table below:
AttributeTypeDescriptionOptional ValuesDetailDefault Value
precisionstringmatching accuracyfirstmatching is considered successful when the initial letter or full pinyin is equal to the current pinyinfirst
startstarting with the current pinyin is considered a successful match
everyfull Pinyin and current Pinyin are strictly equal in order to be considered a successful match
anycontaining all characters of the current pinyin are considered as successful matches
continuousbooleanWhether the matching Chinese indexes need to be continuous in order to be considered successfultruenot requiredfalse
falserequired
spacestringhandling of spaces in Chinese characters and pinyin during matchingignoreignoreignore
preservepreserve
lastPrecisionstringthe accuracy of the last character matchingfirstMatching is considered successful when the initial letter or full pinyin is equal to the current pinyinBy default, when the precision is any, lastPrecision is any; Otherwise lastPrecision is start
startstarting with the current pinyin is considered a successful match
everyfull Pinyin and current Pinyin are strictly equal in order to be considered a successful match
anycontaining all characters of the current pinyin are considered as successful matches
vbooleanWhether to allow using v to match ütrueallow using v to match üfalse
falsenot allow using v to match ü