【问题标题】:How to remove whitespace and new line, only at beginning of textarea如何仅在 textarea 开头删除空格和换行符
【发布时间】:2016-02-13 06:19:37
【问题描述】:

我只想在开头删除空格和换行符。我想要一些建议,我正在使用以下代码。

var myTxtArea = document.getElementById('visionText');
myTxtArea.value = myTxtArea.value.replace(/^s|^n/g, '');

【问题讨论】:

标签: javascript regex


【解决方案1】:

alert(" \n\n    I am a string \n  starting with whitespace and newline");
alert(" \n\n    I am a string with no starting whitespace and newline".replace(/^\s+/, ""));
var reg = /^\s|\n/g;

var str = " asdfkadsf[xxxxx]bb";

var test = str.replace(reg,"") ;

也试试这个

" \n\n    I am a string  ".replace(/^\s+/,"");

【讨论】:

    【解决方案2】:

    好的,您将尝试使用函数 trim,该函数从开头和结尾删除空格和新行

    var myTxtArea = document.getElementById('visionText'); myTxtArea.value = myTxtArea.value.trim();

    【讨论】:

    • 请注意原始问题中非常微妙的提示:only at the beginning
    猜你喜欢
    • 2015-11-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多