【问题标题】:How can I perform an equivalent of php str_replace in JavaScript?如何在 JavaScript 中执行相当于 php str_replace 的操作?
【发布时间】:2013-01-07 17:50:23
【问题描述】:

如何在 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

我想替换所有出现的。

【问题讨论】:

  • RegExp 是你想要的。查看replace() 并省略i 标志。此外,包括g 标志用于全局匹配。
  • str_ireplace 不区分大小写,看起来你在 php 中倒退了

标签: javascript replace str-replace case-sensitive


【解决方案1】:

你可以使用正则表达式:

var str = 'is not';
str = str.replace(/IS/g, 'something'); // won't replace "is"

g 用于全局。

如果您想要不区分大小写的结果,请添加 i 标志 /is/gi

【讨论】:

  • 要真正得到答案,您需要演示g 标志的使用。来自 PHP 的人显然会期待全局替换。您还应该为不区分大小写的场景解释i 标志,以便发布者可以两种方式使用它。
  • > 我想替换所有出现的地方。
  • 我添加了g 标志,忘记了! @bfavaretto OP 要求区分大小写。
  • @bfavaretto 该示例显示了一个敏感的替换,但他的预先解释显示出相反的愿望。我认为OP只是感到困惑。抱歉,关于“阅读 OP”,我猜 OP 有点模棱两可。
  • @Nile 我也花了一段时间才意识到混乱的根源。我刚刚用上面的 OP 替换了我的 cmets。
【解决方案2】:

与 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/

【讨论】:

    猜你喜欢
    • 2016-02-14
    • 1970-01-01
    • 2015-01-19
    • 1970-01-01
    • 2011-05-03
    • 2010-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多