Delete spaces before text only [duplicate] - javascript

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>

Related

JS: how to remove "spammy invisible chars"? [duplicate]

This question already has answers here:
Regular expression for all printable characters in JavaScript
(5 answers)
How to replace non-printable unicode characters (Javascript)
(3 answers)
Closed 2 years ago.
Some user is flooding with some chars that bypass regex filters.
when I paste that chars into UTF8 editor, they look same (except the flood version is not selectable completly: it seems to be some invisible chars inserted
And when you switch to ANSI encodage, you clearly see the difference of the 2 words
liebehomo
lâ€iâ€ebâ€ehâ€oâ€mo
When I paste that spammy word into developer tool, I get
s.length gives 14 and not 9 !
So my question is: how would it be possible to filter these spammy words that contains some strange chars ?
Probably as simple as replacing any non-printable character first:
string = string.replace(/[^ -~]+/g, "");
document.getElementById('demo').addEventListener('input', function(e) {
e.target.innerHTML = e.target.innerHTML.replace(/[^ -~]+/g, "");
console.log(e.target.innerHTML);
});
<textarea id="demo"></textarea>

Remove spaces between paragraphs with regex [duplicate]

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

Javascript: Replace Comma with ### - only with in double quotes [duplicate]

This question already has answers here:
Find comma in quotes with regex and replace with HTML equiv
(4 answers)
Closed 8 years ago.
In the below string,
'This "is, "just, for", Test", ignore it. My name is "FirstName, LastName".'
I want to replace all Commas(,) only inside the double quotes("") with ###.
For now I only found the matching pattern for (""), but need to build the regex to replace the commas.
/".*?"/g
Could you please help me? Thanks in advance ;)
Expected o/p: This "is### "just### for"### Test", ignore it. My name is "FirstName### LastName".
Note: This is not dupe of "Find comma in quotes with regex and replace with HTML equiv". Please see my expected o/p(Even I wanna replace the Comma in inner double quotes).
You can do this using a callback ...
var r = s.replace(/"[^"]+"/g, function(v) {
return v.replace(/,/g, '###');
});

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

How to remove last and first spaces from a string? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I trim a string in javascript?
I need to remove only the last and the first spaces from a generic string.
Example:
var str = " hello world "; // become "hello world"
var str = "ehi, do you come from ? "; // become "ehi, do you come from ?"
var str = "I am from Cina"; // remain "I am from Cina"
How can I do that ?
The trim from jQuery is what you are looking for.
$.trim(' string with spaces at the ends ');
Or plain javascript:
' string with spaces at the ends '.trim()

Categories