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

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

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。

  1. if(url && url.includes("recordId") && url.includes("rt")){
  2. this._geRecordId(url);
  3. }
  4. private async geRecordId(url){
  5. const linkedId = this._getURLValue('recordId', url);
  6. if(!linkedId){
  7. return;
  8. }
  9. }
  10. private _getURLValue(name, url) {
  11. if (!url) url = location.href;
  12. name = name.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]");
  13. var regexS = "[\\;]"+name+"=([^;]*)";
  14. var regex = new RegExp(regexS);
  15. var results = regex.exec(url);
  16. return results == null ? null : results[1];
  17. }

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.

  1. if(url && url.includes("recordId") && url.includes("rt")){
  2. this._geRecordId(url);
  3. }
  4. private async geRecordId(url){
  5. const linkedId = this._getURLValue('recordId' , url);
  6. if(!linkedId){
  7. return;
  8. }
  9. private _getURLValue( name, url ) {
  10. if (!url) url = location.href;
  11. name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  12. var regexS = "[\\;]"+name+"=([^;]*)";
  13. var regex = new RegExp( regexS );
  14. var results = regex.exec( url );
  15. return results == null ? null : results[1];
  16. }

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:

确定