1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > JS实现拼音搜索汉字(支持首字母匹配)

JS实现拼音搜索汉字(支持首字母匹配)

时间:2020-07-13 03:50:54

相关推荐

JS实现拼音搜索汉字(支持首字母匹配)

JS 拼音匹配

如果对你项目有帮助麻烦在github上点个star

详细的实现过程,发布在掘金,麻烦掘金点个赞 如何实现一个高效的拼音匹配库

仓库: /xmflswood/pinyin-match

介绍

能够使用拼音快速检索目标。

28.5KB (gzip => 20.3KB)支持多音字、拼音首字母匹配,具备分词功能覆盖 6763 个汉字返回位置信息,可用于高亮匹配字符在长多音字串下依然有高性能

在线演示:http://laosep.top/pinyin-match;

安装

npm install pinyin-match --save

也支持<script>引入

<script src="pinyin-match/dist/main.js"></script>

PinyinMatch.match('xxx', 'x')

API

.match(input, keyword)

查询匹配拼音的数据。

参数:

input{string}目标字符串keyword{string}输入的拼音或其他关键词

返回:

{[Array]|{Boolean}}

使用范例

列表项为字符串:

const PinyinMatch = require('pinyin-match');let test = '123曾经沧海难为水除却巫山不是云'PinyinMatch.match(test, '23曾'); // [1, 3]PinyinMatch.match(test, 'cjc') // [3, 5]PinyinMatch.match(test, 'cengjingcanghai') // [3, 6]PinyinMatch.match(test, 'cengjingcangha') // [3, 6]PinyinMatch.match(test, 'engjingcanghai') // falsePinyinMatch.match(test, 'zengjingcang') // [3, 5]PinyinMatch.match(test, 'sdjkelwqf') // falsePinyinMatch.match(text, 'zengji ng cang') // [3, 5]PinyinMatch.match(text, 'zengji ng cangsdjfkl') // falsePinyinMatch.match(' 我 爱你 中 国 ', 'nzg') // [6, 12]PinyinMatch.match(' 我 爱你 中 国 ', '爱你中') // [5, 8]

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。