Remove spaces between paragraphs with regex [duplicate] - javascript

This question already has answers here:
JavaScript: how to use a regular expression to remove blank lines from a string?
(5 answers)
Closed 4 years ago.
some code here
some code here
to
some code here
some code here
What is the regex to remove the spaces between each paragraph? I've been searching for a while but I couldn't find one. Some of the results would be:
some code heresome code here
but it isn't the one i'm trying to find

Replace multiple \n with single \n:
var str = `some code here
some code here`;
console.log(str);
str = str.replace(/\n{2,}/g,'\n');
console.log(str);

replace 1 or more \n (newline) characters with a single newline character:
const input = `some code here
some code here
some code here
some code here
some code here`;
const output = input.replace(/\n+/g, '\n');
console.log(output);

Related

Regex match pattern more than once in the same line [duplicate]

This question already has answers here:
Regular expression to stop at first match
(9 answers)
Closed 7 months ago.
I am trying to perform this transformation to a string (using javascript):
Input:
[hello]{world}and[good]{night}
Output:
<span class="top">hello<span class="bottom">world</span></span>and<span class="top">good<span class="bottom">night</span></span>
To do that I am using the following regex:
text.replace(/\[(.*)\]\{(.*)\}/gim, "<span class='top'>$1<span class='bottom'>$2</span></span>")
It works correctly when only setting one occurrence of the pattern in a string [hello]{world}
But if I add a string with more than one, the regex matches the first [] and the last {} instead, and prints this:
<span class='top'>hello]{world}and[good<span class='bottom'>night</span></span>
How can I tell regex to match the first pattern and the second pattern instead of matching it as one bigger pattern?
Note that between the [] and {} I expect there to be no text. So [hello]world and good{night} should not be matched.
You need to put ? after .* to make the quantifier lazy, instead of greedy.
const text = '[hello]{world}and[good]{night}'
const result = text.replace(/\[(.*?)\]\{(.*?)\}/gim, "<span class='top'>$1<span class='bottom'>$2</span></span>")
console.log(result)

How to use regular expression to trim the beginning of a line to a specific symbol? [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 3 years ago.
They send me a text file that starts with unnecessary information and then what is needed goes further. How to remove the beginning to a specific symbol.
Example: line = 'lots of text {text needed}';
It is necessary to delete everything before the symbol {. I tried the regular expression option below:
let str = /^[^{]+/.exec(line)[0];
but it returns the beginning of the text to the symbol {
I need to do the opposite
Thanks for any help
This is how you can do with JavaScript.
var str = 'lots of text {text needed}';
str = str.substring(str.indexOf("{") + 1);
console.log(str);

Regexp Get Text Between parenttheis [duplicate]

This question already has answers here:
Regular Expression to get a string between parentheses in Javascript
(10 answers)
Closed 4 years ago.
I have string like this:
var str = "Hello (World) I'm Newbie";
how to get World from string above using RegExp?, I'm sorry I don't understand about regex.
Thank's
Rather than using a regex - use .split()...Note the escaped characters in the splits. The first split gives "World) I'm Newbie" and the second gives "World".
var str = "Hello (World) I'm Newbie";
var strContent = str.split('\(')[1].split('\)')[0];
console.log(strContent); // gives "World"
Assuming that there will be atleast one such word, you can do it using String#match. The following example matches the words between parentheses.
console.log(
"Hello (World) I'm Newbie"
.match(/\(\w+\)/g)
.map(match => match.slice(1, -1))
)
This might help you for your regex
\w match whole world
+ plus with another regex
[] starts group
^ except
(World) matching word
var str = "Hello (World) I'm Newbie";
var exactword=str.replace(/\w+[^(World)]/g,'')
var filtered = str.replace(/(World)/g,'')
alert(exactword)
alert(filtered)

Delete spaces before text only [duplicate]

This question already has answers here:
how do I strip white space when grabbing text with jQuery?
(4 answers)
Closed 7 years ago.
How can I delete spaces before only text:
" with spaces between"
I need:
"some text with spaces between"
All I have found it's text.replace(/\s/g, '') but it doesn't work well:
"sometextwithspacesbetween"
and indexOf(' ') + 25 also isn't good solve, because I have a different text and number of spaces before it.
If it possible, help me please
Set the reg exp to look for the beginning of the string
text.replace(/^\s+/, '')
You can use trim() in javascript and jQuery.trim(string) in jQuery. Both of them remove the whitespace from the beginning and end of a string.
var str = " some text with spaces between";
console.log(str);
console.log(str.trim());
console.log(jQuery.trim(str));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Regex struct in Javascript [duplicate]

This question already has answers here:
How to convert unicode in JavaScript?
(4 answers)
Closed 9 years ago.
I am getting data returned in a JSON file with Unicode characters. How to replace Unicode characters as per my example?
\u003cli class=\"channels-content-item\"\u003e\n
\n
\u003cdiv class=\"shmoovie-content-cell\"\u003e\n
\u003ca href=\"\/movie\/the-makeover\" class=\"ux-thumb-wrap contains-addto yt-uix-sessionlink\" data-sessionlink=\"ei=Oo21UdLqM8aDhgHc_IHYCA\"\u003e
After replacing with regex:
\u003c must be replaced by <
\u003e must be replaced by >
\/ must be replaced by /
\" must be replaced by "
How to do that?
Using the bit of the string you posted I put this fiddle together that shows how to just use the string value you have (like in this SO answer I mentioned in the comments).
HTML
<div id="content"></div>
JS
var s = "\u003cli class=\"channels-content-item\"\u003e\n\n\u003cdiv class=\"shmoovie-content-cell\"\u003e\n\u003ca href=\"\/movie\/the-makeover\" class=\"ux-thumb-wrap contains-addto yt-uix-sessionlink\" data-sessionlink=\"ei=Oo21UdLqM8aDhgHc_IHYCA\"\u003e";
var div = document.getElementById('content');
div.innerHTML = s;
console.log(s);
Which sets the HTML content for the div with the elements:
<li class="channels-content-item">
<div class="shmoovie-content-cell">
<a href="/movie/the-makeover" class="ux-thumb-wrap contains-addto yt-uix-sessionlink" data-sessionlink="ei=Oo21UdLqM8aDhgHc_IHYCA">
Although it's not valid HTML, javascript seems to figure it out, at least it does in Chrome.
Not sure that regex is the best idea...The below solves the problem using the replace function built into JavaScript.
DEMO: http://jsfiddle.net/abc123/5DFUb/1/
var str = "\u003cli class=\"channels-content-item\"\u003e\n \n \u003cdiv class=\"shmoovie-content-cell\"\u003e\n \u003ca href=\"/movie/the-makeover\" class=\"ux-thumb-wrap contains-addto yt-uix-sessionlink\" data-sessionlink=\"ei=Oo21UdLqM8aDhgHc_IHYCA\"\u003e";
var newStr = str.replace("\\u003c", "<").replace("\\u003e",">").replace("\\/","/").replace("\\\"","\"");
alert(newStr);

Categories