2014-09-03 119 views
1

你好朋友:)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中做类似的事情? 我这样做是为了降低服务器的性能,所以我想通过浏览器来完成这项工作。因为在一个演出页面中替换的内容太多了。

在此先感谢。

回答

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'; 
+0

我已经知道,但我需要的是一个功能,同时运行所有的转。这是否意味着我必须一遍又一遍地做,而不映射对象?这是最后一个? – kefy 2014-09-03 08:10:57

+0

因为它也是工作,所以我会将您的答案投票。 :) – kefy 2014-09-03 08:16:30

+0

我添加了代码,向您展示如何复制PHP的str_replace方法。 – 2014-09-03 08:19:42

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); 
+0

这是不是一个数组,虽然,你很可能与jquery.each修复它() – 2014-09-03 08:08:40

+0

我已经知道,但我需要的是同时运行所有转弯的功能。这是否意味着我必须一遍又一遍地做,而不映射对象?这是最后一个? – kefy 2014-09-03 08:09:52

+0

使用each()方法 – Florin 2014-09-10 11:35:44

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

+0

伟大的人,这是我寻找的。 Wonderfull和很多谢谢:) – kefy 2014-09-03 08:14:46

1

参考phpjs.org其中有PHP函数JavaScript实现。

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); 

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

+0

请尽量避免仅链接答案而添加相关代码。 – 2014-09-03 08:27:10

+0

@hinata:我在复制和粘贴代码时遇到了格式问题。所以只是给了链接。从现在开始,请记住这一点。 – 2014-09-03 08:51:43