0

I'm trying to remove the whitespaces from a textarea . The below code is not appending the text i'm selecting from two dropdowns. Can somebody tell me where i'd gone wrong? I'm trying to remove multiple spaces within the string as well, will that work with the same? Dont know regular expressions much. Please help.

    function addToExpressionPreview() {
  var reqColumnName = $('#ddlColumnNames')[0].value;
  var reqOperator = $('#ddOperator')[0].value;

  var expressionTextArea = document.getElementById("expressionPreview");

  var txt = document.createTextNode(reqColumnName + reqOperator.toString());
  if (expressionTextArea.value.match(/^\s+$/) != null)
  {
    expressionTextArea.value = (expressionTextArea.value.replace(/^\W+/, '')).replace(/\W+$/, '');

  }
  expressionTextArea.appendChild(txt);
}
2

3 Answers 3

1
> function addToExpressionPreview() {   
>   var reqColumnName = $('#ddlColumnNames')[0].value;
>   var reqOperator = $('#ddOperator')[0].value;

You might as well use document.getElementById() for each of the above.

>   var expressionTextArea = document.getElementById("expressionPreview");
>   var txt = document.createTextNode(reqColumnName + reqOperator.toString());

reqOperator is already a string, and in any case, the use of the + operator will coerce it to String unless all expressions or identifiers involved are Numbers.

>   if (expressionTextArea.value.match(/^\s+$/) != null) {

There is no need for match here. I seems like you are trying to see if the value is all whitespace, so you can use:

if (/^\s*$/.test(expressionTextArea.value)) {
  // value is empty or all whitespace

Since you re-use expressionTextArea.value several times, it would be much more convenient to store it an a variable, preferably with a short name.

>     expressionTextArea.value = (expressionTextArea.value.replace(/^\W+/,
> '')).replace(/\W+$/, '');

That will replace one or more non-word characters at the end of the string with nothing. If you want to replace multiple white space characters anywhere in the string with one, then (note wrapping for posting here):

expressionTextArea.value = expressionTextArea.value.
                             replace(/^\s+/,'').
                             replace(/\s+$/, '').
                             replace(/\s+/g,' ');

Note that \s does not match the same range of 'whitespace' characters in all browsers. However, for simple use for form element values it is probably sufficient.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm able to replace the whitespaces but the appendchild() isnt working
Capitalisation counts: element.appendChild(document.createTextNode('sometext'));
0

Whitespace is matched by \s, so

expressionTextArea.value.replace(/\s/g, "");

should do the trick for you.

In your sample, ^\W+ will only match leading characters that are not a word character, and ^\s+$ will only match if the entire string is whitespace. To do a global replace(not just the first match) you need to use the g modifier.

2 Comments

Now the replace method is working but the resultant text appended to the expressionTextArea is not working . Do you know why?
how can a 'text' not 'work'? 'expressionTextArea.value = expressionTextArea.value.replace(/\s+/g, '');' will replace the value of the textarea.
0

Refer this link, you can get some idea. Try .replace(/ /g,"UrReplacement");

Edit: or .split(' ').join('UrReplacement') if you have an aversion to REs

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.