How to Remove Character ↵ in between two words of JavaScript String?

remove character from string python
remove character from string excel
how to remove characters in excel from right
how to remove characters from a string
remove character from string c
remove character from string java
remove specific character from string excel
how to remove characters after a hyphen in excel

If I select two lines together and get text using window.getSelection().toString() then I get in between two words (last word from the upper line, and the first word of the lower word). Below is my code-

document.addEventListener('dblclick',function (event) {

var element = document.getElementsByClassName(event.target.parentElement.parentElement.parentElement.className);


var T= window.open("", "MsgWindow", "width=200,height=500");

T.document.write(element[0].innerText.replace(/\n/g, "<br/>"));


T.document.write(`<script>
window.addEventListener("keydown", function(e){
if(e.keyCode === 16) {
var text = "";
if (window.getSelection) {
text_1 = window.getSelection().toString();
text = text_1.replace(/\u21b5/g," "); // my attempt to corrrect
                        } 
words = text.split(" ");
console.log(words);
}
},false);
</script>`);
},false);

For example, if I select -

Sides

Section

and press shift key I get -

Array(1)
0: "Sides↵Section"
length: 1
__proto__: Array(0)

but the out put should be-

Array(1)
0: "Sides Section"
length: 1
__proto__: Array(0)

How can I replace with space?

PS: I have tried some possibilities based on solutions given on SE, but it didn't work, plz don't consider it trivial or duplicate post.

You can replace new lines and carriage returns and then replace empty spaces.

window.addEventListener(
  "keydown",
  function(e) {
    if (e.keyCode === 16) {
      var text = "";
      if (window.getSelection) {
        text_1 = window.getSelection().toString();
        text = text_1
                .replace(/\n|\r/g, " ")
                .replace(/\s/, "");
      }
        words = text.split(" ");
        console.log(words);
    }
  },
  false
);

Python Remove Character from String, How remove last characters from string. To remove characters from right of function we will use LEFT and LEN function. Generic Formula to Remove Characters from Right = LEFT (text, LEN (text)-num_of_char) Text: the text from which you want to remove the last n characters in excel

I think you'll want to replace new lines and carriage returns

So something like this:

text = text_1.trim().replace(/[\n\r]/g, ' ');

UPDATE

// Solution from code sample 

  T.document.write(`<script>
    window.addEventListener("keydown", e => {
      if (e.keyCode === 16) {
        if (window.getSelection) {
          const text = window.getSelection().toString();
          const words = text.trim().replace(/\r?\n/g, " ");
          console.log(words);
        }
      }
  });
  </script>`);

Java Remove Character from String, Given a string. Write a program to remove all the occurrences of a character in the string. Examples: Input : s = "geeksforgeeks" c = 'e' Output : s = "gksforgks"  Remove first x characters from the beginning of the text strings: 1. Hold down the Alt + F11 keys to open the Microsoft Visual Basic for Applications window. 2. Click Insert > Module, and paste the following code in the Module Window. 1 2 3 Public Function removeFirstx (rng As 3. Then go back to

Just replace \s+ (any whitespace of one or more characters) with " ". You can also split on the same regex, if that was your goal.

document.addEventListener('select', () => {
  if (window.getSelection) {
    text = window.getSelection().toString();
    words = text.trim().replace(/\s+/g, " ");
    console.log(words);
  }
});
<textarea rows=4>
foo
bar
</textarea>

Remove Characters From Right, This will remove the character from the string. This method removes the first occurrence of the string. Syntax: string.replace('characterToReplace', '');. Example​:. To remove specific unwanted characters in Excel, you can use a formula based on the substitute function. In the example shown, the formula in C4 is: = SUBSTITUTE(B4,CHAR(202),"") Which removes a series of 4 invisible characters at the start of each cell in column B.

You could replace \n and \r\n with space. Depending which one the OS uses. If you don't want to bother with OS check then replace \r\n first then \n.

UPDATE:

The code is: text = text_1.replace(/\r?\n/g, " ");

Remove all occurrences of a character in a string, You can also use the StringBuilder class which is mutable. StringBuilder sb = new StringBuilder(inputString);. It has the method deleteCharAt() , along with many  5 Ways to Remove a Character from String in Python 1. Removing a Character from String using the Naive method 2. Removal of Character from a String using replace() Method 3. Removal of Character from a String using Slicing and Concatenation 4. Removal of Character from a String using join() method

How to remove a character from string in JavaScript ?, For most of Excel users, sometimes, you need to delete first n characters from the beginning of the text strings or remove the last x characters from the end of the  String.Remove () method removes a given number of characters from a string at a specified position. The position is a 0 index position. That means, the 0th position is the first character in the string. In C#, Strings are immutable.

How to remove single character from a String, how-to-remove-a-particular-character-from-a-string with method signature and examples of concat, compare, touppercase, tolowercase, trim, length, equals, split  Java Remove Character from String. replace( char oldChar, char newChar) : This method returns a new string where oldChar is replaced with newChar. This method replaces all the replace(CharSequence target, CharSequence replacement) : This method replaces the target substring with the replacement

How To Remove First, Last X Characters Or Certain Position , In the result, it has returned the last three characters by removing the first character from the actual text string. but how this formula works. First of all, we have used  The String.TrimEnd method removes characters from the end of a string, creating a new string object. An array of characters is passed to this method to specify the characters to be removed. The order of the elements in the character array does not affect the trim operation. The trim stops when a character not specified in the array is found.

Comments
  • developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • I have tried, but didn't get the result I wanted
  • That is just how the newline character is displayed, there are many ways to remove it. Try variable.replace("\n", " ")
  • @GammaGames I tried that to, you can check yourself, that does not work, I do't know the reason why!
  • @Mike people are trying to be helpful and your comments can be read as being rude.
  • I think using trim before replacing would be a better solution than filtering after
  • @GammaGames Changed the code to two replaces. trim doesn't work in that case.
  • Plz use my complete code, and see whether it works or not, I have tried your line but it didn't work
  • @Mike are you able to provide a fiddle or code sandbox of the code that is not working in your example and I can add my changes to that
  • you copy my code and put it in inspect->console it will run... I tried your code but it didn't work, plz change my complete code, also I am new so i am not familiar with the things you mentioned.
  • could you plz put it to above code and check whether it works or not ?