Line break in react text area being treated as space wrongly and can be removed with trim()

huangapple go评论72阅读模式
英文:

Line break in react text area being treated as space wrongly and can be removed with trim()

问题

以下是您要翻译的内容:

I have a text area like below which I try to remove the leading and trailing spaces of the string but I want to include the number of new line charactor in the string.

const [textValue, setTextValue] = useState('')

const onChangeValue= ({target: {value}}) => {
    console.log(value.length);
    console.log(value.trim().length);
    console.log(value.replace(/^\s+|\s+$/g, '').length);
    setTextValue(value);
};

<textarea
     onChange={onChangeValue}
     noAutoResize
     width={'100%'}
     height={'15em'}
     value={textValue}
     maxLength={maxLength}
/>

If I keep pressing the enter button on the textArea to add line breaks, the value.length keep increasing but value.trim().length always remains in 0.

According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim, the trim() method removes whitespace only

Why it it also remove my line break? Even using regex it has the same behavior. it seems the state is storing a space instead.

Is there any way to achieve such requirement?

英文:

I have a text area like below which I try to remove the leading and trailing spaces of the string but I want to include the number of new line charactor in the string.

const [textValue, setTextValue] = useState(&#39;&#39;)

const onChangeValue= ({target: {value}}) =&gt; {
    console.log(value.length);
    console.log(value.trim().length);
    console.log(value.replace(/^\s+|\s+$/g, &#39;&#39;).length);
    setTextValue(value);
};

&lt;textarea
     onChange={onChangeValue}
     noAutoResize
     width={&#39;100%&#39;}
     height={&#39;15em&#39;}
     value={textValue}
     maxLength={maxLength}
/&gt;

If I keep pressing the enter button on the textArea to add line breaks, the value.length keep increasing but value.trim().length always remains in 0.

According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim, the trim() method removes whitespace only

Why it it also remove my line break? Even using regex it has the same behavior. it seems the state is storing a space instead.

Is there any way to achieve such requirement?

答案1

得分: 0

Whitespace is defined as white space characters plus line terminators

更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim

your case should use regexp:

function trimSpace(str) {
  return str.replace(/^[ \t]+|[ \t]+$/g, '')
}

and call


const onChangeValue= ({target: {value}}) => {
    console.log(value.length);
    console.log(trimSpace(value));
    setTextValue(value);
};

英文:

Whitespace is defined as white space characters plus line terminators

More info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim

your case should use regexp:

function trimSpace(str) {
  return str.replace(/^[ \t]+|[ \t]+$/g, &#39;&#39;)
}

and call


const onChangeValue= ({target: {value}}) =&gt; {
    console.log(value.length);
    console.log(trimSpace(value));
    setTextValue(value);
};

huangapple
  • 本文由 发表于 2023年2月24日 11:52:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75552462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定