.includes方法在字符串上不会检测小写字母。

huangapple go评论62阅读模式
英文:

Why .includes on string doesn't detect lower case letters?

问题

I wrote this to detect if the url contain this particular keyword i.e. recordId but if url contains it in lower i.e. recordid then it doesn't detect.

如何使它们都被检测到,因为数据可以包含recordid或recordId。

if(url && url.includes("recordId") && url.includes("rt")){
    this._geRecordId(url);
} 

private async geRecordId(url){
    const linkedId = this._getURLValue('recordId', url);
    if(!linkedId){
        return;
    }
}

private _getURLValue(name, url) {
    if (!url) url = location.href;
    name = name.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]");
    var regexS = "[\\;]"+name+"=([^;]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(url);
    return results == null ? null : results[1];
}

I tried changing regex.

英文:

I wrote this to detect if the url contain this particular keyword i.e. recordId but if url contains it in lower i.e. recordid then it doesn't detect.

How do I make them detect both because data can have both recordid or recordId.

if(url && url.includes("recordId") && url.includes("rt")){
      this._geRecordId(url);
    } 

 private async geRecordId(url){
    const linkedId = this._getURLValue('recordId' , url);
    if(!linkedId){
      return;
    }

private _getURLValue( name, url ) {
    if (!url) url = location.href;
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\;]"+name+"=([^;]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( url );
    return results == null ? null : results[1];
 }

I tried changing regex

答案1

得分: 1

你可以使用带有 i 标志的正则表达式,这样它将匹配大写和小写字母。

/recordId/i.test(url)

英文:

You can write a regex with the i flag, so that it will match both uppercase and lowercase letters.

/recordId/i.test(url)

huangapple
  • 本文由 发表于 2023年4月13日 14:53:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定