Replace double new line character with only one? - javascript

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

Related

JavaScript Regex - add ~ symbol between lettes except letter after space

how can I put ~ symbol between letters except the first letter after space? Here is what I got from this site by trying here and there.
txt = text.split(/ +?/g).join(" ");
txt.replace(/(.)(?=.)/g, "$1~")
this regex output as
input = "hello friend"
output = "h~e~l~l~o~ ~f~r~i~e~n~d"
How to output "h~e~l~l~o~ f~r~i~e~n~d"?
Use \S instead of . when matching the character you want to insert a ~ next to - \S matches any non-space character, whereas . matches any non-newline character.
const text = 'hello friend';
const singleSpaced = text.split(/ +/g).join(" ");
const output = singleSpaced.replace(/(\S)(?=.)/g, "$1~");
console.log(output);
or
const text = 'hello friend';
const output = text
.replace(/ +/g, ' ')
.replace(/(\S)(?=.)/g, "$1~");
console.log(output);
You can do this in one operation with a replace using this regex:
(?<=\S)(?!$)
It matches the position after a non-whitespace character (?<=\S) except for the position at end of string (?!$). You can then insert a ~ at those positions:
text = "hello friend"
txt = text.replace(/(?<=\S)(?!$)/g, '~')
console.log(txt)
You could match a single non whitespace char, and assert that to the right is not optional whitespace chars followed by the end of the string.
In the replacement use the full match followed by a tilde $&~
\S(?!\s*$)
See a regex demo.
input = "hello friend"
output = input.replace(/\S(?!\s*$)/g, '$&~')
console.log(output)

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);

Remove all special characters from string in JS

I want to remove all special characters from string and add only one "-"(hyphen) in the place.
Consider below example
var string = 'Lorem%^$&*&^Ipsum#^is#!^&simply!dummy text.'
So, from the above string, if there is a continuous number of special characters then I want to remove all of them and add only one "-" or if there is a single or double special character then also that should be replaced by "-"
Result should be like this
Lorem-Ipsum-is-simply-dummy text-
I have tried below, but no luck
var newString = sourceString.replace(/[\. ,:-]+/g, "-");
You could use .replace to replace all non-alphabetical character substrings with -:
const input = 'Lorem%^$&*&^Ipsum#^is#!^&simply!dummy text.';
const output = input.replace(/[^\w\s]+/gi, '-');
console.log(output);
If you want to permit numbers too:
const input = 'Lorem123%^$&*&^654Ipsum#^is#!^&simply!dummy text.';
const output = input.replace(/[^\w\s\d]+/gi, '-');
console.log(output);

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);

Regular Expression: Any character that is not a letter or number

I need a regular expression that will match any character that is not a letter or a number. Once found I want to replace it with a blank space.
To match anything other than letter or number you could try this:
[^a-zA-Z0-9]
And to replace:
var str = 'dfj,dsf7lfsd .sdklfj';
str = str.replace(/[^A-Za-z0-9]/g, ' ');
This regular expression matches anything that isn't a letter, digit, or an underscore (_) character.
\W
For example in JavaScript:
"(,,#,£,() asdf 345345".replace(/\W/g, ' '); // Output: " asdf 345345"
You are looking for:
var yourVar = '1324567890abc§$)%';
yourVar = yourVar.replace(/[^a-zA-Z0-9]/g, ' ');
This replaces all non-alphanumeric characters with a space.
The "g" on the end replaces all occurrences.
Instead of specifying a-z (lowercase) and A-Z (uppercase) you can also use the in-case-sensitive option: /[^a-z0-9]/gi.
This is way way too late, but since there is no accepted answer I'd like to provide what I think is the simplest one: \D - matches all non digit characters.
var x = "123 235-25%";
x.replace(/\D/g, '');
Results in x: "12323525"
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Match letters only /[A-Z]/ig
Match anything not letters /[^A-Z]/ig
Match number only /[0-9]/g or /\d+/g
Match anything not number /[^0-9]/g or /\D+/g
Match anything not number or letter /[^A-Z0-9]/ig
There are other possible patterns
try doing str.replace(/[^\w]/);
It will replace all the non-alphabets and numbers from your string!
Edit 1: str.replace(/[^\w]/g, ' ')
Just for others to see:
someString.replaceAll("([^\\p{L}\\p{N}])", " ");
will remove any non-letter and non-number unicode characters.
Source
To match anything other than letter or number or letter with diacritics like é you could try this:
[^\wÀ-úÀ-ÿ]
And to replace:
var str = 'dfj,dsf7é#lfsd .sdklfàj1';
str = str.replace(/[^\wÀ-úÀ-ÿ]/g, '_');
Inspired by the top post with support for diacritics
source
Have you tried str = str.replace(/\W|_/g,''); it will return a string without any character and you can specify if any especial character after the pipe bar | to catch them as well.
var str = "1324567890abc§$)% John Doe #$#'.replace(/\W|_/g, ''); it will return str = 1324567890abcJohnDoe
or look for digits and letters and replace them for empty string (""):
var str = "1324567890abc§$)% John Doe #$#".replace(/\w|_/g, ''); it will return str = '§$)% #$#';
Working with unicode, best for me:
text.replace(/[^\p{L}\p{N}]+/gu, ' ');

Categories

Resources