@hubxu/utils / Exports / dom/html
▸ escapeHtml(str
): string
转义HTML字符串中的特殊字符
此方法会将以下字符转义为HTML实体:
- & -> &
- < -> <
-
-> >
- ' -> '
- " -> "
Example
escapeHtml('<div class="test">')
返回: <div class="test">
Name | Type | Description |
---|---|---|
str |
string |
要转义的HTML字符串 |
string
转义后的字符串
▸ extractImgSrc(html
, callback?
): string
提取并处理HTML字符串中的图片标签
Description
此方法用于扫描HTML字符串中的所有img标签,并可以通过回调函数对每个图片的src进行处理。 主要功能包括:
- 使用正则表达式匹配所有img标签
- 提取每个img标签的src属性和其他属性
- 通过回调函数可以自定义处理每个图片的src
- 如果回调函数返回新的src值,则会替换原始src
Example
const html = '<div><img src="old.jpg" alt="test"/></div>';
const result = extractImgSrc(html, (imgTag, src) => {
return src.replace('old.jpg', 'new.jpg');
});
result: '<div><img src="new.jpg" alt="test"/></div>'
Name | Type | Description |
---|---|---|
html |
string |
需要处理的HTML字符串 |
callback? |
(imgDomString : string , src : string , index : number , attrs : Record <any , any >) => undefined | string |
可选的回调函数,用于处理每个找到的图片标签 |
string
处理后的HTML字符串
▸ getCharLength(str
): number
| string
[]
获取字符串的实际字符长度
此方法会:
- 优先使用 Intl.Segmenter API 计算字符长度(如果浏览器支持)
- 如果不支持,则使用 Array.from 将字符串转为数组并过滤掉不可见字符
- 过滤掉空字符串
Example
const str = 'abc哈哈🫣🫵👨';
const length = getCharLength(str);
length: 8
Name | Type | Description |
---|---|---|
str |
string |
要计算长度的字符串 |
number
| string
[]
使用Segmenter时返回数字,否则返回过滤后的字符数组
▸ templateDataReplace(template
, data
): string
模板字符串数据替换函数
此方法用于替换模板字符串中的变量占位符为实际数据。 主要功能包括:
- 支持 {{variable}} 形式的变量占位符
- 从传入的数据对象中查找对应的变量值
- 自动替换所有匹配的占位符
- 对空模板字符串进行校验
Throws
当模板字符串为空或类型错误时抛出错误
Example
const template = 'Hello {{name}}, your age is {{age}}';
const data = { name: 'Tom', age: 18 };
const result = templateDataReplace(template, data);
result: 'Hello Tom, your age is 18'
Name | Type | Description |
---|---|---|
template |
string |
包含变量占位符的模板字符串 |
data |
Record <any , any > |
包含变量值的数据对象 |
string
替换变量后的结果字符串
▸ unescapeHtml(str
): string
将转义后的HTML实体还原为原始字符
此方法会将以下HTML实体还原为原始字符:
- & -> &
- < -> <
- > -> >
- ' -> '
- " -> "
Example
unescapeHtml('<div class="test">')
返回: <div class="test">
Name | Type | Description |
---|---|---|
str |
string |
包含HTML实体的字符串 |
string
还原后的原始字符串