【问题标题】:Replace String with empty string用空字符串替换字符串
【发布时间】:2014-07-23 10:13:17
【问题描述】:

我有一个可以有以下子字符串值的字符串(也包括引号)

"+form"
"+ form "
" form+"
"form +"
+ form
+form
form+
form +
form   +
+    form
....
....
.... or just simply 'form' that doesnt surrounded by double quotes

问题是找到与以下匹配的子字符串(+形式或形式+)

  • '+ form' 不能用引号括起来
  • '+'和'form'之间的空格数不限

    如果找到了,那么应该用空字符串“”代替

    Input:
    ' form+ "form" +  form '
    Output:
    "form"
    

    输入: ' 形式' 输出: '形式'

有什么帮助吗?

我只是初学者,似乎无法通过简单的替换和方法索引来解决这个问题:-(

var abc = string.replace(" " + "");
 if(abc.indexOf("+form") > -1 || abc.indexOf("form+") > -1 || abc.indexOf("form") > -1 || abc.indexOf("\"+form\"") > -1 || || abc.indexOf("\"form+\"") > -1 )
 {
    // then what should do?
 }

【问题讨论】:

  • 可以把你已经试过的JS代码加进去吗?
  • 你从哪里得到这些输入? IE。真正的意图是什么?
  • 我正在创建一个应用程序,用户输入例如(“字符串”+字符串)的代码,我需要单独解释。即字符串表示变量,“字符串”表示实际字符串
  • 使用正则表达式可能是更好的选择..

标签: javascript string replace indexof


【解决方案1】:

希望对你有帮助

var input = ' form   + " form " +  form "  form +  " " +   form "'; 
var tmp = input.replace(new RegExp(/\"?\s*form\s*\"?\s*\+\s*\"?/g), ''); //form +
tmp = tmp.replace(new RegExp(/\"?\s*\+\s*form\s*\"?\s*\"?/g), '');// + form
alert(tmp);

必须创建两个正则表达式,一个用于 + 在“form”之前的情况,另一个用于它之后的情况。可以有更好的方法。下面的一些注释有助于理解。

\s* -> zero or more spaces  
\"? -> zero or one double quote  
\+  -> one + symbol  
/../g -> replace all matches

【讨论】:

    【解决方案2】:

    试试这个:

     var abc = string.replace(" ", "");
    if (abc.indexOf("+form") > -1 {
        abc.replace("+form", "");
    }
    else if (abc.indexOf("form+") > -1) {
        abc.replace("form+", "");
    }
    else if (abc.indexOf("form") > -1) {
        abc.replace("form", "");
    }
    else if (abc.indexOf("\"+form\"") > -1) {
        abc.replace("\"+form\"", "");
    }
    else if (abc.indexOf("\"form+\"") > -1)) {
    abc.replace("\"form+\"", "");
    }
    

    或者:

    var array = new Array();
    array.push("+form", "form+", "form", "\"+form\"", "\"form+\"");
    for (var i = 0; i < array.length; i++) {
    if (abc.indexOf(array[i]) > -1) {
        abc.replace(array[i], "");
    }
    

    虽然使用正则表达式可能会更好。

    【讨论】:

    • 它不是递归的,我需要修改后的原始字符串。上面的代码只能修改修剪后的字符串
    • 如果你在上面的代码中输入'form+ "form" + form',你会得到"form"作为输出。试试我做的编辑
    【解决方案3】:

    你应该试试正则表达式。我知道它们很难创建,甚至更难阅读,但请在这个网站上尝试其中的一些:http://www.regexr.com/

    这里是如何使用它们:http://www.w3schools.com/js/js_regexp.asp

    但是,如果您不喜欢使用正则表达式,您可以尝试像 Nejc Lovrencic 所说的那样创建您的有限状态机。

    我尝试了一个小的正则表达式并得到了这个:

    /[ \t\n\r+]+[^"]\w+[^"][ \t\n\r+]*/g

    它有什么作用?好吧,它会尝试查找 + 或空字符(如果我遗漏了任何空白字符,请纠正我)没有引号、包含多个字符的单词、没有引号、+ 或空字符,它将全局查找,意味着在每一行上。

    只寻找“形式”会更容易:

    /\"\w+\"/g

    这会查找引号中的单词。

    第一种情况使用 replace() 函数将这些单词替换为空字符串。

    var result = intputString.replace("/[ \t\n\r+]+[^"]\w+[^"][ \t\n\r+]*/g", "");

    引号内的单词加上空字符串将存储在结果中。

    如果您使用第二种情况,您可以轻松使用:

    var regExp = /\"\w+\"/g var result = regExp.exec(intputString)

    引号中的单词将存储在结果中。

    这些是我在几分钟内完成的正则表达式,但我相信您可以得出更具体的结果。

    编辑:在第一种可能的情况下复制了错误的正则表达式,现在应该没问题了。

    【讨论】:

      猜你喜欢
      • 2014-11-12
      • 2012-08-09
      • 2013-01-04
      • 1970-01-01
      • 1970-01-01
      • 2020-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多