[英]Jquery replace multiple string in element, not replace element
Hello good friends :) 你好好朋友:)
I have a question about jquery. 我有一个关于jquery的问题。 Is there a function in jquery to replace the many words that have been mapped within an object or array.
jQuery中是否有一个函数来替换已在对象或数组中映射的许多单词。
Example in php: 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;
?>
it will be produce 它会产生
PHP is powerfull open source programming language,
there many websites around the world that are built using php
How do I do a similar thing in jquery? 我如何在jquery中做类似的事情? I did this to reduce the performance of the server so I thought to do this work by the browser.
我这样做是为了降低服务器的性能,因此我想通过浏览器来完成这项工作。 Because the contents fr replace in one show page is too much.
因为在一个显示页面中替换的内容太多。
Thanks in advance. 提前致谢。
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);
You don't need jQuery, just use Javascript's replace method. 您不需要jQuery,只需使用Javascript的replace方法即可。
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');
You can replicate PHP's str_replace method like so: 您可以像这样复制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';
Refer phpjs.org which has javascript implementations of php functions. 请参阅phpjs.org ,它具有php函数的javascript实现。
The code for str_replace is given below. 下面给出了str_replace的代码。 Source : http://phpjs.org/functions/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];
}
Usage: 用法:
$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
will have PHP is powerfull open source programming language,there many websites around the world that are built using php
$textAfter
将拥有PHP is powerfull open source programming language,there many websites around the world that are built using php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.