简体   繁体   English

变量数组+正则表达式不能一起工作?

[英]variable array + regular expression not working together?

im making a small smiley script , what it does is to change ::1:: into an image html for a div. 我做了一个小的笑脸脚本,它的作用是将:: 1 ::改为图像html为div。

the code as follow: 代码如下:

var smileys = {
  '1': 'http://domain.com/smiley1.gif',
  '2': 'http://domain.com/smiley2.gif',
  '3': 'http://domain.com/smiley3.gif'
};

function checksmileys(){   
x$('.message').each(function() 
  var start = '<img src="';
  var end = '">';
  x$(this).html( x$(this).html().replace(/::(\d+)::/g, start + smileys['$1'] + end) );
});

Checksmileys function is triggered by user event. Checksmileys功能由用户事件触发。

However it is not able to get the digit(which is the id) out of a sentence. 但是它无法从句子中获取数字(即id)。 It kept on producing this <img src="undefined"> 它继续产生这个<img src="undefined">

My HTML example as follows: 我的HTML示例如下:

<div id="chat">
   <ul>
     <li class="message">Hi john</li>
     <li class="message">what are you doing</li>
     <li class="message">::1:: nothing</li>
     <li class="message">hi</li>
     <li class="message">nice to meet you ::1::</li>
   </ul>
</div>

Where is my problem here? 我的问题在哪里?

I guess you need a function here: 我想你需要一个函数:

html = html.replace(/::(\d+)::/g, function($0, $1) { return start + smileys[$1] + end })

here's when the functional form of html() comes in handy 这是html()的功能形式派上用场的时候

$(this).html(function(_, oldhtml) { 
    return oldhtml.replace(/::(\d+)::/g, function($0, $1) { 
        return start + smileys[$1] + end; 
    })
})

In JavaScript, property names don't have prefixes like they often do in PHP. 在JavaScript中,属性名称没有像PHP中经常使用的前缀。 The name of the property you create in your smileys object is 1 , not $1 , so change smileys['$1'] to smileys['1'] . 您在smileys对象中创建的属性的名称是1 ,而不是$1 ,因此将smileys['$1']更改为smileys['1']

Update : From your comment below, it seems you're trying to use $1 to refer to the capture group. 更新 :从下面的评论中,您似乎正在尝试使用$1来引用捕获组。 That only works when $1 is part of the string you pass in, but the expression start + smileys['$1'] + end is evaluated before the call to replace and then passed into it. 仅当$1是您传入的字符串的一部分时才有效,但表达式start + smileys['$1'] + end在调用replace 之前进行评估,然后传递给它。 smileys['$1'] will be undefined. smileys['$1']将不确定。

Since you're trying to do a lookup, your best bet is to pass in a function that does the lookup: 由于您正在尝试进行查找,最好的办法是传入一个执行查找的函数:

x$(this).html( x$(this).html().replace(/::(\d+)::/g, function(m, c1) {
    return start + smileys[c1] + end;
}) );

c1 is the text of the first capture group. c1是第一个捕获组的文本。 More 更多


You've called it an array, which I'm guessing is because you're used to the term "associative array" from PHP. 你把它称为一个数组,我猜是因为你习惯了PHP中的术语“关联数组”。 Your smileys object is just an object, not an array. 你的smileys对象只是一个对象,而不是一个数组。 In JavaScript, when we say "array", we mean something created via [] or new Array , which has specific handling for a certain class of property names (numeric ones), a special length property, and some handy array-like functions. 在JavaScript中,当我们说“数组”时,我们指的是通过[]new Array创建的东西,它具有特定类属性名称(数字)的特定处理,特殊length属性和一些方便的类数组函数。 What you've done with smileys is perfectly fine and completely normal, we just tend not to use the word "array" for it ("map" is more common in the JS world). 你使用smileys所做的完全正常,完全正常,我们倾向于不使用“数组”这个词(“地图”在JS世界中更常见)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM