【问题标题】:Remove white space inside single quotes before and after text inside the quotes删除引号内文本前后的单引号内的空格
【发布时间】:2023-03-26 06:42:02
【问题描述】:

我正在使用一个名为 QueryBuilder 的第三方 JavaScript 插件。

问题是保存后无法修剪输入,因此数据正在保存

testName='   test   '

这是我的 javascript 代码,它正在删除我不想要的所有空格,我试图在所有文本之前和之后删除单引号中的空格。很像修剪,但修剪不起作用,所以我需要一个正则表达式来替换方法

get_condition_sql__str = $.trim(get_condition_sql.sql);
                get_condition_sql__clean = get_condition_sql__str.replace(/\s/g, '')
                console.log(get_condition_sql__clean);
                jQuery('.exception_conditions__sql').val(get_condition_sql__clean);

【问题讨论】:

    标签: javascript jquery regex


    【解决方案1】:

    在字符串结尾前准确地查找一个'

    const input = `testName='   test   '`;
    const cleaned = input.replace(/ +(?=[^']*'$)/g, '');
    console.log(cleaned);

    输入中只有一个单词,但如果您需要在引号内的单词之间保留空格,请交替匹配空格两侧的 '

    const input = `testName='   test test2   '`;
    const cleaned = input.replace(/' +| +'/g, "'");
    console.log(cleaned);

    【讨论】:

    • 第二个效果很好,像我需要的那样保留了中间的空间并从最后删除,但唯一的小问题是它留下了前面的空间。不过还是更好,但最抱怨的是最后的空间,所以非常感谢!
    • 运行 sn-p - 它会根据需要删除前面的空间。
    【解决方案2】:

    请用这个来删除剩余空间:

    <script type="text/javascript">
     
    var original_str3 = "   This is a string"
     
    //Strips all space to the left of the string
    alert( original_str3.trimLeft() + ' <--- Without any spaces on the left' );
     
    </script>
    

    或者用这个来删除右边的空间:

    <script type="text/javascript">
     
    var original_str4 = "This is a string   "
     
    //Strips all space to the right of the string
    alert( original_str4.trimRight() + ' <--- Without any spaces on the right' );
    

    对于从两侧字符或字符串中删除空格:

    <script type="text/javascript">
     
    var original_str2 = "S    t    r  in  g"
    //Strips excessive white spaces i.e. retains only one space between each letter
    var white_space_stripped_str = original_str2.replace(/\s+/g, ' ');
     
    alert(white_space_stripped_str + ' <---- With exactly one space between each letter in case each letter has multiple spaces');
     
    </script>
    

    如果有其他需要,请告诉我。

    例子:

    <!DOCTYPE html>
    <html>
    <body>
    
    <p>Click the button to replace "blue" with "red" in the paragraph below:</p>
    
    <p id="demo">"vSourceCountry = 'TEST'"</p>
    
    <button onclick="myFunction()">Try it</button>
    
    <script>
    function myFunction() {
      var str = `testName='   test test2   '`; 
      var res = str .replace(/' +| +'/g, "'");
      document.getElementById("demo").innerHTML = res;
    }
    </script>
    
    </body>
    </html>
    

    结果:

    Click the button to replace "Microsoft" with "W3Schools" in the paragraph  below:
    
        testName='test test2'
    

    或者您可以根据需要使用等:

    https://www.w3schools.com/jsref/jsref_replace.asp
    

    【讨论】:

    • 唯一的问题是字符串实际上是像 example = "vSourceCountry = 'TEST'" 这样存储的,所以修剪不会修剪单引号内的双引号
    • @MichaelDaSilva 感谢这个用户:!DOCTYPE html>

      点击下面段落中的“蓝色”替换为“红色”:

      "vSourceCountry = 'TEST'"

    • @MichaelDaSilva 谢谢如果它对我们有好处,请告诉我如果需要其他更改forums.asp.net/t/…
    猜你喜欢
    • 2014-05-25
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多