【问题标题】:Find a keyword in a Div, then get the string after colon, and turn it to a variable在一个Div中找到一个关键字,然后得到冒号后面的字符串,然后转成一个变量
【发布时间】:2016-12-24 06:19:13
【问题描述】:

试图弄清楚如何在 jquery、javascript 或 php 中执行此操作... 我有一个 div 或一个 p 标签,里面有文本。
例如:

<p>John likes math.  John sid: CA1001 Susie likes English: Susie sid: CA1002</p>

我要做的是首先在段落或 div 标记中找到所有 sid:(s)。每找到一个,我想获取冒号前面的 id 字符串,其中 id 将有 6 个字符。然后,将该 id 字符串转换为变量,这样我就可以将 id 号替换为一个链接。
例如:

<a href='http://www.website.com/studentinformation?id=$studentid'>CA1001</a>

类似的东西。因此,每次在该 div 中出现 id 时,我的代码都会将 id 转换为链接。这可能吗??

【问题讨论】:

  • 这是可能的。请记住,虽然 JS 可以工作,但这也意味着禁用 JS 的用户将无法获得链接。因此,我建议在服务器端进行。现在,问题是:这些字符串是从哪里来的?如果您要生成这些,我建议首先从它们中生成链接,而不是在第二步中进行替换。
  • 字符串来自哪里
  • 作为其他 cmets - 根据需要生成 p,例如 &lt;p data-sid='CA1001'&gt;... 但听起来您正在解析其他一些首先生成 p 的数据 - 看那里。

标签: javascript php jquery html search


【解决方案1】:

您可能想使用正则表达式。

在您的情况下,您说所有 sid 都采用以下格式

sid:
space
6 digits or chacters

所以正则表达式会是这样的/sid: (.{6})/g

之后,您需要将 p 或 div 或其他元素中的文本替换为正则表达式可以找到的链接和 ID。

var pattern = /sid: (.{6})/g; // pattern to match sid: 000000
$('p').each(function(i,e) {
    // find matches and replace
    $(e).html($(e).html().replace(pattern, '<a href="http://www.website.com/studentinformation?id=$1">$1</a>'));
});

Fiddle working demo - 这不是优化的,只是简单的代码。

【讨论】:

  • 他说了 6 个字符,并举了一个或两个例子,比如“John sid: CA1001”。你需要稍微调整一下这个正则表达式。
【解决方案2】:

您需要:

  1. 获取所有需要更新的段落
  2. 提取段落内容
  3. 查找所有要替换的子字符串
  4. 提取 ids,构建链接 html 并用这些链接替换 ​​3 中的子字符串

查看演示 - https://fiddle.jshell.net/ermakovnikolay/uu9ka8gt/ 和以下代码:

function replacer(match) {
   var m = match.split(' ')[1];
   return '<a href="http://www.website.com/studentinformation?id=' + m + '">' + m + '</a>';
}

$('p').each( function(index, paraElement) {
   paraElement.innerHTML = paraElement.innerHTML.replace(/sid:.{7}/gi, replacer );
});

【讨论】:

  • 到目前为止效果很好......即使“sid”是 SiD、SID、siD 等,是否有一种更快的方法来替换“sid”,而不是用一个替换它们一个????
  • 是的,您需要将i 标志添加到正则表达式,这样它就会忽略大小写,如果您是这个意思。我更新了小提琴 - https://fiddle.jshell.net/ermakovnikolay/uu9ka8gt/
【解决方案3】:

如果你想在服务器端处理它,我会提供一个 PHP 解决方案。

<?php 
// This can be all of your text after you compile it or you could loop
$in = "<p>John likes math.  John sid: CA1001 Susie likes English: Susie sid: CA1002</p>";

// Set up what we're finding and what we will replace it with
$pattern = '/sid: (.{6})/'; 
$replace = 'sid: <a href="http://www.website.com/studentinformation?id=$1">$1</a>'; 

// Filter the text with our parameters
$out = preg_filter( $pattern, $replace, $in ); 

// Dump to the browser
echo $out;

【讨论】:

    【解决方案4】:

    太棒了...谢谢...试图让我的记忆重新回到编码上...我会在 javascript 和服务器端尝试...

    现在,如果我想将它放在页面上的一个 div 中...我可能需要使用该 div 标签创建一个 id,例如:

     <div id="test">This is my text that has sid: CA0056<br /> This is my text that is second has another sid: CA0055</div> 
    

    那么我在javascript中要做的就是...

     var src_str = $("#test");
     var theHtml = src_str.html();
    

    这对 javascript 是否正确????如何在 php 中的特定 div 上?

    【讨论】:

    • var pattern = /sid: (.{6})/g; src_str.html(stc_str.html().replace(pattern, '&lt;a href="http://www.website.com/studentinformation?id=$1"&gt;$1&lt;/a&gt;'));
    猜你喜欢
    • 1970-01-01
    • 2015-03-15
    • 2014-02-10
    • 2020-02-13
    • 2017-09-27
    • 1970-01-01
    • 2018-07-28
    • 1970-01-01
    • 2020-04-18
    相关资源
    最近更新 更多