Skip to main content

正则

番风Less than 1 minute

我有个网页链接 https://github.com/iuap-design/blog/issues/99?href=sffsfsfopen in new window 怎么在 js 中正则匹配到 sffsfsf

https://playcode.io/1889028open in new window

function getHrefValue(url) {
    // 创建正则表达式,匹配 href 参数及其值
    const regex = /href=([^&]+)/;
    //const regex = /href=([^&]{0,3})/;//如果限制长度
    const match = url.match(regex);
    // 检查是否有匹配,并返回结果
    if (match) {
        return decodeURIComponent(match[1]);
    }
    return null; // 如果没有找到匹配项,返回 null
}
// 测试 URL
const url = "https://github.com/iuap-design/blog/issues/99?href=sffsfsf";
const hrefValue = getHrefValue(url);
console.log(hrefValue); // 输出: sffsfsf

同时包含数字和字母的用户名 长度在 8-15 之间 正则

/^(?!\d+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{8,15}$/

匹配所有的空格,给所有空格去除空格

value.replace(/\s/g, "");