Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to replace line breaks in ACE ? #2869

Closed
unix4you2 opened this issue Jan 29, 2016 · 18 comments
Closed

How to replace line breaks in ACE ? #2869

unix4you2 opened this issue Jan 29, 2016 · 18 comments

Comments

@unix4you2
Copy link

Hi!

It's very common for me to process data, arrays and another information that is in different lines, but when I'm trying to replace line breaks (at the end of a line) I dont have how to say to ACE Editor that I want to look for line breaks and replace it for another text or even for including more line breaks, for example.

I'm not talking about <br> or \n... I mean the simple invisible character at the end of a code line.

Is there any way to do that ? To search and replace special chars?

Thanks a lot. Regards.

@chimmihc1
Copy link

The invisible character at the end of a code line is \n (or \r\n for windows).

You can search (and replace) them with regex.

@unix4you2
Copy link
Author

Hi @chimmihc1 thanks for your answer and time.

I tried using \n, \n\r and the combination of buttons in the top of the search & replace dialog as you can see in the screen shot link in this message to try what you say and Ace editor dones't search for the line break hidden chars...

ScreenShot here

Another idea? Thanks a lot for your time!

@unix4you2
Copy link
Author

Hi again @chimmihc1

I found that using $ character in regular expression Ace find for all line breaks.

But now my question is: what should I write in the replace box to replace a line break for example, for two line breaks or another special char?

See here a new screenshot!

Thanks!!

@brianharcourt
Copy link

I'm having the same issue. Any news on how to do this?

@JaroslavMoravec
Copy link

Problem still persist. Browser: chrome. Line breaks are found, but it can't be replaced. I want to replace two line breaks for one.
Eg.: \n{2} => \n or \n+ => \n

Break lines can be found by $, but I still don't know, how to write break line to replacement string.

@MLeidel
Copy link

MLeidel commented Oct 14, 2019

I used this for strip whitespace at EOLs

 editor.find('[ \t]+$',{
        backwards: false,
        wrap: true,
        caseSensitive: false,
        wholeWord: false,
        regExp: true
    });
    editor.replaceAll('');

@old-df38257622
Copy link

I have the opposite of this. I have a long one liner string of text full of literal "\n" that I want to replace with whitespace. How do i do this?

@MLeidel
Copy link

MLeidel commented Mar 1, 2020

If that's so then you would actually have a new "line" for each "\n"... That's if it is already in the editor.
I'm not sure exactly what the text looks like that you want to replace with space.
If the text is a string you're inserting into Ace then just replace the "\n" using JavaScript before inserting.
Need more info

@old-df38257622
Copy link

Say this is my string:
\n\n\ndata\n\n\nmore data\n\n\n\n\netc...
I want to have it looking like this:



data


more data




etc...

But, it's a ton longer. Replacing all of the "\n"s manually isn't really a choice.

@MLeidel
Copy link

MLeidel commented Mar 3, 2020

Remember Ace is a "Text editor".
For instance, if I open a file with your string in it with any text editor, say Sublime, it will not load like you're wanting it to load. It will load like this: \n\n\ndata\n\n\nmore data\n\n\n\n

You will have to break out each "\n" in your string (or file) into a separate "line"... then Insert or setValue with "that" file into Ace. In other words the way you want it to look is how you will have to bring it into Ace.

Python is a good tool for working with text and text files:

str = "\n\n\ndata\n\n\nmore data\n\n\n\n"
lst = str.split("\n")
for x in lst:
   print(x)  # or write to a file...

Please let me know if there is an easier way to do this.. Great question!

@old-df38257622
Copy link

Good point. I can do this easily in JS. as well, with much the same concept as your code:

var str = "\n\n\ndata\n\n\nmore data\n\n\n\n";
str = str.split("\n");
str.forEach(e => {
  console.log(e) // alternatively go ahead and write to a file, output it somewhere else...
})

@github-actions
Copy link

github-actions bot commented May 9, 2022

This issue has not received any attention in 1 year. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@unix4you2
Copy link
Author

This issue continues in the actual version: 1.4.14

@sekedus
Copy link
Contributor

sekedus commented Jan 16, 2023

Found :

editor.findAll('\n', {
    regExp: true
});

Not Found if \n+ :

editor.findAll('\n+', {
    regExp: true
});


ACE parses line-by-line: https://stackoverflow.com/questions/35289002/ace-editor-multiline-regex

var line = session.getLine(row);

ranges = lang.getMatchOffsets(session.getLine(i), this.regExp);

Copy link

This issue has not received any attention in 1 year. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@bakomon
Copy link

bakomon commented Jan 17, 2024

This issue continues in version: 1.32.3

@metrixsam
Copy link

metrixsam commented Dec 29, 2024

It's been a month now since this pull request. Is this issue fixed yet? Why didn't @nlujjawal finish reviewing this request and merge it yet?

@akoreman
Copy link
Contributor

PR #5675 has been merged and released as part of Ace 1.39.0. With that version you should now be able to replace line breaks in regex mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests