Jquery替换元素中的多个字符串,而不是替换元素

时间:2014-09-03 07:59:28

标签: javascript php jquery

你好朋友:)

我对jquery有疑问。在jquery中是否有一个函数来替换已在对象或数组中映射的许多单词。

php中的示例:

<?php 
    $textBefore  = "PHP is amazing open source programmer language, 
                    there many webs around the world that are built using php";
    $forReplace  = array("amazing","programmer","webs");
    $replacement = array("powerfull","programming","websites");
    $textAfter   = str_replace( $forReplace , $replacement , $textBefore);
    echo $textAfter;
?>

它将产生

PHP is powerfull open source programming language, 
there many websites around the world that are built using php

我如何在jquery中做类似的事情? 我这样做是为了降低服务器的性能,所以我想通过浏览器来完成这项工作。因为一个节目页面中的内容fr替换太多了。

提前致谢。

4 个答案:

答案 0 :(得分:2)

var value = $("#text").val(); // value = 9.61 use $("#text").text() if you are not on select box...
value = value.replace(".", ":"); // value = 9:61
// can then use it as
$("#anothertext").val(value);

答案 1 :(得分:1)

你可以用一个循环来实现这个目标。

var text = "This is some sample text.";
var target = ['some', 'text'];
var replace = ['replaced', 'sentence'];

for(var i = 0; i < replace.length; i++){
    text = text.replace(target[i], replace[i])
}

Demo

答案 2 :(得分:1)

你不需要jQuery,只需使用Javascript替换方法。

var text  = 'PHP is amazing open source programmer language, there many webs around the world that are built using php';

text = text.replace('amazing', 'powerful');
text = text.replace('programmer', 'programming');
text = text.replace('webs', 'websites');

您可以像这样复制PHP str_replace方法:

function str_replace(search, replace, subject) {
    for (i=0; i<replace.length; i++) {
        subject = subject.replace(search[i], replace[i]);
    }
    return subject;
}

var subject = "This is a test string.";
var search = ['test', 'string'];
var replace = ['cool', 'thang'];

subject = str_replace(search, replace, subject);

\\ subject is now 'This is a cool thang';

答案 3 :(得分:1)

参考具有php函数的javascript实现的phpjs.org

str_replace的代码如下。资料来源:http://phpjs.org/functions/str_replace/

function str_replace(search, replace, subject, count) {
  var i = 0,
    j = 0,
    temp = '',
    repl = '',
    sl = 0,
    fl = 0,
    f = [].concat(search),
    r = [].concat(replace),
    s = subject,
    ra = Object.prototype.toString.call(r) === '[object Array]',
    sa = Object.prototype.toString.call(s) === '[object Array]';
  s = [].concat(s);
  if (count) {
   this.window[count] = 0;
  }

  for (i = 0, sl = s.length; i < sl; i++) {
    if (s[i] === '') {
      continue;
    }
    for (j = 0, fl = f.length; j < fl; j++) {
      temp = s[i] + '';
      repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
      s[i] = (temp)
    .split(f[j])
    .join(repl);
      if (count && s[i] !== temp) {
        this.window[count] += (temp.length - s[i].length) / f[j].length;
      }
    }
  }
  return sa ? s : s[0];
}

用法:

$textBefore  = "PHP is amazing open source programmer language,there many webs around the world that are built using php";
$forReplace  = ["amazing","programmer","webs"];
$replacement = ["powerfull","programming","websites"];
$textAfter   = str_replace( $forReplace , $replacement , $textBefore);

$textAfterPHP is powerfull open source programming language,there many websites around the world that are built using php