如何在 JavaScript 中执行等效的 php str_replace
How can I perform an equivalent of php str_replace in JavaScript?
如何在 JavaScript 中执行区分大小写的字符串替换?我正在寻找类似于PHP str_replace
的东西。
var text = "this IS some sample text that is going to be replaced";
var new_text = replace_in_javascript("IS", "is not", text);
document.write(new_text);
应该给
this is not some sample text that is going to be replaced
我想替换所有出现的事件。
您可以使用正则表达式:
var str = 'is not';
str = str.replace(/IS/g, 'something'); // won't replace "is"
g
是全球性的。
如果要获得不区分大小写的结果,请添加i
标志,/is/gi
。
与 PHP 函数完全相同:
function str_ireplace(search, replace, subject) {
// discuss at: http://phpjs.org/functions/str_ireplace/
// original by: Martijn Wieringa
// input by: penutbutterjelly
// input by: Brett Zamir (http://brett-zamir.me)
// improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// improved by: Jack
// bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// bugfixed by: Onno Marsman
// bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// bugfixed by: Philipp Lenssen
// example 1: str_ireplace('l', 'l', 'HeLLo');
// returns 1: 'Hello'
// example 2: str_ireplace('$', 'foo', '$bar');
// returns 2: 'foobar'
var i, k = '';
var searchl = 0;
var reg;
var escapeRegex = function(s) {
return s.replace(/(['''^'$*+'[']?{}.=!:(|)])/g, '''$1');
};
search += '';
searchl = search.length;
if (Object.prototype.toString.call(replace) !== '[object Array]') {
replace = [replace];
if (Object.prototype.toString.call(search) === '[object Array]') {
// If search is an array and replace is a string,
// then this replacement string is used for every value of search
while (searchl > replace.length) {
replace[replace.length] = replace[0];
}
}
}
if (Object.prototype.toString.call(search) !== '[object Array]') {
search = [search];
}
while (search.length > replace.length) {
// If replace has fewer values than search,
// then an empty string is used for the rest of replacement values
replace[replace.length] = '';
}
if (Object.prototype.toString.call(subject) === '[object Array]') {
// If subject is an array, then the search and replace is performed
// with every entry of subject , and the return value is an array as well.
for (k in subject) {
if (subject.hasOwnProperty(k)) {
subject[k] = str_ireplace(search, replace, subject[k]);
}
}
return subject;
}
searchl = search.length;
for (i = 0; i < searchl; i++) {
reg = new RegExp(escapeRegex(search[i]), 'gi');
subject = subject.replace(reg, replace[i]);
}
return subject;
}
原文:http://phpjs.org/functions/str_ireplace/
相关文章:
- 使用php或javascript从facebook相册URL中删除多余的部分
- 将PHP变量传递给jQuery时遇到问题
- 通过javascript重定向html传递php变量
- 将数组从PHP传递到Javascript
- 如何在php文件中获取$.post-ajax传递的值
- Javascript运行php文件,然后下载文件
- PHP中的setcookie仅适用于localhost
- 如何使用jquery处理php循环通过元素
- 通过Ajax将JavaScript函数传递给PHP文件
- 而循环只设置php中输入字段中的第一个值
- 我需要从php调用javascript或jquery
- 如何通过php页面将数据库值检索到jquery自动完成框中
- PHP AJAX图片上传示例不上传
- 通过php页面中的js强制下载txt
- 如何在PHP中使用$_POST获取Select元素值
- 我想使用模态通过php文件发送邮件,并且我希望在提交关闭后关闭pop
- 如何在php变量中嵌入JQuery代码
- 请解释一下 - xmlhttp.open(“GET”,“gethint.php?q=”+str,true);.
- NodeJS md5'字节串'比如PHP md5(str,true)
- Regex match javascript/php function name in str