How to split multiline string into lines? - javascript

How to split a multiline string into lines without taking into account the newline in the line itself?
My approach is not working:
const str = `
Hello
World\t
its\nbeautiful
`;
JSON.stringify(str).split(/$\\n/g)
What is the result of this approach:
[""", "Hello", "World\t", "its", "beautiful", """]
What result is needed in the end:
[""", "Hello", "World\t", "its\nbeautiful"]

Since \n is the character that marks new lines, just like a normal new line, there is no way for javascript to differenciate between \n and \n.
An idea for your "special" new lines could be to escape that \n with another \ so you would end up with
const str = `
Hello
World\t
its\\nbeautiful
`;
str.split("\n");
The result would be this:
['', 'Hello', 'World\t', 'its\\nbeautiful', '']

Related

How to remove duplicate \n (line break) from a string and keep only one?

I have a string like this:
This is a sentence.\n This is sentence 2.\n\n\n\n\n\n This is sentence 3.\n\n And here is the final sentence.
What I want to is:
This is a sentence.\n This is sentence 2.\n This is sentence 3.\n And here is the final sentence.
I want to remove all duplicated \n characters from a string but keep only one left, is it possible to do like that in javascript ?
You may try replacing \n{2,} with a single \n:
var input = "This is a sentence.\n This is sentence 2.\n\n\n\n\n\n This is sentence 3.\n\n And here is the final sentence.";
var output = input.replace(/\n{2,}\s*/g, '\n');
console.log(output);
You can use regex as /\n+/g to replace it with single \n
const str =
"This is a sentence.\n This is sentence 2.\n\n\n\n\n\n This is sentence 3.\n\n And here is the final sentence.";
const result = str.replace(/\n+/g, "\n");
console.log(result);

Find and replace string text between two other string texts JS

I'm trying to find and replace a word in a string
Example:
let string =
`
Title: Hello World
Authors: Michael Dan
`
I need to find the Hellow World and replace with whatever I want, here is my attempt:
const replace = string.match(new RegExp("Title:" + "(.*)" + "Authors:")).replace("Test")
When you replace some text, it is not necessary to run String#match or RegExp#exec explicitly, String#replace does it under the hood.
You can use
let string = "\nTitle: Hello World\nAuthors: Michael Dan\n"
console.log(string.replace(/(Title:).*(?=\nAuthors:)/g, '$1 Test'));
The pattern matches
(Title:) - Group 1: Title: fixed string
.* - the rest of the line, any zero or more chars other than line break chars, CR and LF (we need to consume this text in order to remove it)
(?=\nAuthors:) - a positive lookahead that matches a location that is immediately followed with an LF char and Authors: string.
See the regex demo.
If there can be a CRLF line ending in your string, you will need to replace (?=\nAuthors:) with (?=\r?\nAuthors:) or (?=(?:\r\n?|\n)Authors:).
You might be better off converting to an object first and then just defining the title property:
let string =
`
Title: Hello World
Authors: Michael Dan
`
const stringLines = string.split('\n');
let stringAsObject = {};
stringLines.forEach(
(line) => {
if (line.includes(':')) {
stringAsObject[line.split(':')[0]] = line.split(':')[1];
}
}
);
stringAsObject.Title = 'NewValue';
You can use replace method like that:
string.replace("Hello World", "Test");
I can achieve this without regex. All you need is knowing the index of the string that you need to find.
var original = `
Title: Hello World
Authors: Michael Dan
`;
var stringToFind = "Hello World";
var indexOf = original.indexOf(stringToFind);
original = original.replace(original.substring(indexOf, indexOf + stringToFind.length), "Hey Universe!");
console.log(original)

Replace double new line character with only one?

How could I remove every double new line character with only one?
var string = "this is a ↵↵↵↵ test there will ↵↵ be more ↵↵↵↵ newline characters"
something like this
var string = "this is a ↵↵ test there will ↵ be more ↵↵ newline characters"
I've already tried this, but this replaces all new lines, i want to keep the single ones
string.replace(/[\n\n]/g, '')
string = string.replace(/\n{2}/g, '\n');
console.log(string);
That will do what you explained... but I believe you need this...
string = string.replace(/\n+/g, '\n');
console.log(string);
[\n\n] character class works as Logical OR. [\n\n] this means match \n or \n. what you need is \n followed by \n. So just remove the [] character class.
let str = `this is a
test there will
be more
newline characters`
console.log(str.replace(/\n\n/g, '\n'))
console.log(str.replace(/\n+/g, '\n')) // <--- simply you can do this

JS show special ascii characters in a string

Is there a way to make this string:
foo
bar
Appear like this:
foo\r\n\tbar
It would really help with debugging a lexer.
The key is to escape \n in a string replace.
let specialCharacters = [
{regex: /\n/g, replacement: '\\n'},
{regex: /\t/g, replacement: '\\t'}
];
function escapeSpecialCharacters(str){
specialCharacters.forEach(c => {
str = str.replace(c.regex, c.replacement);
});
return str;
}
console.log(escapeSpecialCharacters(`test
test
test
1234`));
If debugging is all you want to do, you can display all escaped characters it in the browser console, but putting the string into an array:
let string = "test\ntest";
let arr = [];
arr.push(string);
console.log(arr);
But that will not change your string in any way, it still contains new line, instead of "\n".

Split string by all spaces except those in parentheses

I'm trying to split text the following like on spaces:
var line = "Text (what is)|what's a story|fable called|named|about {Search}|{Title}"
but I want it to ignore the spaces within parentheses. This should produce an array with:
var words = ["Text", "(what is)|what's", "a", "story|fable" "called|named|about", "{Search}|{Title}"];
I know this should involve some sort of regex with line.match(). Bonus points if the regex removes the parentheses. I know that word.replace() would get rid of them in a subsequent step.
Use the following approach with specific regex pattern(based on negative lookahead assertion):
var line = "Text (what is)|what's a story|fable called|named|about {Search}|{Title}",
words = line.split(/(?!\(.*)\s(?![^(]*?\))/g);
console.log(words);
(?!\(.*) ensures that a separator \s is not preceded by brace ((including attendant characters)
(?![^(]*?\)) ensures that a separator \s is not followed by brace )(including attendant characters)
Not a single regexp but does the job. Removes the parentheses and splits the text by spaces.
var words = line.replace(/[\(\)]/g,'').split(" ");
One approach which is useful in some cases is to replace spaces inside parens with a placeholder, then split, then unreplace:
var line = "Text (what is)|what's a story|fable called|named|about {Search}|{Title}";
var result = line.replace(/\((.*?)\)/g, m => m.replace(' ', 'SPACE'))
.split(' ')
.map(x => x.replace(/SPACE/g, ' '));
console.log(result);

Categories

Resources