【问题标题】:I need help getting a number from a <p> element with javascript我需要帮助使用 javascript 从 <p> 元素中获取数字
【发布时间】:2021-04-01 17:02:04
【问题描述】:

我是 JavaScript 新手。我在&lt;p&gt; 标签中有 3 个 10 位数字,我想选择它们并将它们转换为链接。我能够获得数字并将它们转换为链接,但是我同时获得了所有 3 个数字和一个链接,如果我能得到一些帮助,那就太好了,谢谢

这是我的代码:

<p>
What 0123456789 exactly is this Worker thread module, 9876543210 and why do we need it? 9768352461 In this post, we will talk about the historical reasons concurrency is implemented in JavaScript and Node.js, the problems we might find, current solutions, and the future of parallel processing with Worker threads.
</p>

<script>
function myFunction() {
  var str = document.querySelector("p").innerHTML; 
  var link = 'https://www.google.com/search?q='+text;
  var rgx = /\d{10}/g;
  var text = str.match(rgx);
  var res = str.replace(rgx, '<a href= "'+link+'">"'+text+'"</a>');
  document.querySelector("p").innerHTML = res;
}

myFunction();

</script>

【问题讨论】:

  • 我在

    标签中有 3 个 10 位数字的数字 <p> element。 HTML 只有一种数据类型:string。如果您需要在 JavaScript 中将字符串视为数字,则需要对其进行转换。

  • 你说得对,它们是字符串。我只想获取字符串,但仅将其中一个字符串显示为链接,而不是所有匹配的字符串。

标签: javascript arrays regex function dom


【解决方案1】:

您可以在正则表达式中捕获某些表达式。然后,您可以使用$ 和组号从该组中获取价值。只有一组所以在这种情况下会有$1;

function myFunction() {
  var str = document.querySelector("p").innerHTML;
  var link = "https://www.google.com/search?q=$1";
  var rgx = /(\d{10})/g; // () wraps an expression in a group
  var text = str.match(rgx);
  var res = str.replace(rgx, '<a href= "' + link + '">$1</a>');
  document.querySelector("p").innerHTML = res;
}

更多关于捕获组:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Groups_and_Ranges

编辑:我发现了一个更简单的方法——如果你想引用整个表达式,你不需要创建一个组。要插入匹配值,您可以使用$&amp;:

function myFunction() {
  var str = document.querySelector("p").innerHTML;
  var link = "https://www.google.com/search?q=$&";
  var rgx = /\d{10}/g;
  var text = str.match(rgx);
  var res = str.replace(rgx, '<a href= "' + link + '">$&</a>');
  document.querySelector("p").innerHTML = res;
}

【讨论】:

  • 感谢您的回答。我也会尝试这个选项,总是很高兴知道不同的方式,谢谢
【解决方案2】:
str.match(rgx); 

返回匹配字符串的数组。如果您只想在数组的项目上使用,您可以引用索引(如text[0])或者您可以使用text.forEach循环遍历数组

或者你可以映射它并生成这样的链接:

let links = text.map((string) => { return `www.yourlink.com/${string}`})

【讨论】:

    【解决方案3】:

    在你的替换语句中使用文本而不是 rgx

    var res = str.replace(text, '<a href= "'+link+'">"'+text+'"</a>');
    

    这样你只替换匹配文本的实例而不是所有 rgx 匹配

    编辑 应该这样做

    function myFunction() {
      var str = document.querySelector("p").innerHTML; 
      var rgx = /\d{10}/g;
      var text = str.match(rgx);
      
      for (i=0; i<text.length; i++) { 
         var link = 'https://www.google.com/search?q='+text[i];
         str = str.replace(text[i], '<a href= "'+link+'">"'+text[i]+'"</a>');
      }
      
      document.querySelector("p").innerHTML = str;
    }
    
    myFunction();
    

    【讨论】:

    • 我试过了,我总是得到这个响应 index.html:28 Uncaught TypeError: text.replace is not a function at myFunction (index.html:28) at index.html:32 myFunction @index.html:28(匿名)@index.html:32
    • 不知道为什么会出现错误。我添加了一个完整的示例,它测试了上面的代码,它适用于您提供的示例。
    • 对不起,我没有看到上面的代码。你是对的,它工作得很好,谢谢你的帮助,这正是我想要的,谢谢
    猜你喜欢
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    • 2020-12-03
    • 2010-12-30
    • 2019-04-24
    • 2022-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多