insert line break in tweet from URL? - javascript

So I have a script like this now:
popUp("https://twitter.com/intent/tweet?text=" + greeting + poem + " -&url=" + siteURL, 704, 260);
The "poem" is a haiku and I'd love to have it like:
line1
line2
line3
rather than line 1 // line 2 // line 3, which it is now. I tried inserting stuff like \n in there to no avail. "Poem" is constructed simply like line1 + " // " + line2 ...

As you've guessed, newline characters cannot appear in URLs.
Using a random escaping mechanism won't do you any good; you need to URL-encode the newline:
https://twitter.com/intent/tweet?text=abc%0adef

Creates a line break on twitter if you're posting from the HTML form. You'll need to remove the spaces in bewteen the characters, I couldn't figure out how to make the answer not escape to a newline. Irony alert

Related

How to create whitespace inside a JSON

I want to put a line of space or blanks between the values. Because they're all leaving together right now.
My example:
data: JSON.stringify({
"sessionID":xxxxx,
"synchronize":false,
"sourceRequest":{
"numberOrigin":xxxxxx,
"type":"x",
"description":test + "\\n" + test2 "\\n" + test3 "\\n" + test4,
"userID":xxxxxxxx,
"contact":{
"name":"xxxxxxxxxxxxxxxxx",
"phoneNumber":"xxxxxxxxxx",
"email":xxxx,
"department":"xxxxx"
},
The "\\n" says to put a literal \n in the string - 2 chars. You should just use "\n" to say that its a new line - 1 char.
Note if viewing in Windows Notepad, \n is not enough for a new line.
A simple ' ' (space character) is enough to do what is needed, the json key does hold a string after all, if you need something more prominent you can use '\t', refer here for more.

Regex to remove white spaces, blank lines and final line break in Javascript

Ok guys, I'm having a hard time with regex..
Here's what I need... get a text file, remove all blank lines and white spaces in the beginning and end of these lines, the blank lines to be removed also include a possible empty line at the end of the file (a \n in the end of the whole text)
So my script was:
quotes.replace(/^\s*[\r\n]/gm, "");
This replaces fairly well, but leaves one white space at the end of each line and doesn't remove the final line break.
So I thought using something like this:
quotes.replace(/^\s*[\r\n]/gm, "").replace(/^\n$/, "");
The second "replace" would remove a final \n from the whole string if present.. but it doesn't work..
So I tried this:
quotes.replace(/^\s*|\s*$|\n\n+/gm, "")
Which removes line breaks but joins some lines when there is a line break in the middle:
so that
1
2
3
4
Would return the following lines:
["1", "2", "34"]
Can you guys help me out?
Since it sounds like you have to do this all in a single regex, try this:
quotes.replace(/^(?=\n)$|^\s*|\s*$|\n\n+/gm,"")
What we are doing is creating a group that captures nothing, but prevents a newline by itself from getting consumed.
Split, replace, filter:
quotes.split('\n')
.map(function(s) { return s.replace(/^\s*|\s*$/g, ""); })
.filter(function(x) { return x; });
With input value " hello \n\nfoo \n bar\nworld \n",
the output is ["hello", "foo", "bar", "world"].

RegEx: Match odd amount of repeat between other characters in each line

I was wondering how to match and replace an odd amount of slashes (\) in every line at javascript.
They are used in escaping a string, but sometimes the string is wrapped into lines, so the slash have to move to the next line.
Here is an example: http://regex101.com/r/iI9vO9
I want to match the lines which are marked via "Yes" and ignore the lines marked with "No".
For Example:
"Yes 1\" +
"No 2\\" +
"Yes 3\\\" +
"No 4\\\\" +
"No"
Should be changed to:
"Yes 1" +
"\No 2\\" +
"Yes 3\\" +
"\No 4\\\\" +
"No"
Notice there is characters before and after the slashes in each line, and the slash is moved to the next line when it is repeated an odd time.
I couldn't get it working with (\\)(\\\\)* or look-around.
This is what I have in mind if this work:
text.replace(/([^\\])\\" \+ \n"(.)/gm, '$1\\$2"+ \n "')
If this is not possible with RegEx, I would appreciate any other way to make this possible.
Thanks for your help.
EDIT:
For whoever look this up on Google, this is exactly what solves the problem:
text.replace(/([^\\])((\\{2})*)\\" \+ \n"/g, '$1$2" + \n"\\')
http://jsfiddle.net/5mGWF/1/
This seems to do what you want:
text = text.replace(/([^\\])((\\{2})*)\\\n/g, "$1$2\n\\")
http://jsfiddle.net/5mGWF/

Javascript REgExp. Need remove all words on the line from after ":" to ","

Javascript REgExp. I want remove all words on the line after : to ,
Names of the properties must stay.
I tryed this
var sostavRegexp = $(".post .sostav_box").text();
sostavRegexp = sostavRegexp.replace(/:(.*),*/, ' aaaaaaa');
alert(sostavRegexp);
But this changed all words in line from first ":" in line to last "," in line to one word "aaaaaaa".
I have this line
Мука: 200гр, Сахар: 200гр, Маргарин: 100гр,
Need this:
Мука, Сахар, Маргарин
I`m designer, not a programmer. Help me please :)
Use the reluctant/lazy quantifier. This will do the match between the first , after : rather than the last.
// g is for global replace
.replace(/:.*?,/g, ",")

JavaScript Replace - u0009 .... with .replace(/\u0009/g,'');

I'd like to use Javascript to replace all instances of \u009 in a string
This doesn't seem to be working: .replace(/\u0009/g,'');
Do I need to escape something?
First, the question says "replace all instances of \u009 in a string".
But, the regex has replace(/\u0009/g,''); Is this a typo (different number of zeroes)?
Anyway, if the string only contains, unicode, horizontal tab characters (just one char), then the regex is fine.
If it actually contains 6 ascii chars, then the regex needs to be escaped, like so:
var oneChar = 'Pre \u0009 post';
var sixChars = 'Pre \\u0009 post';
//-- NOTE: If not using Firebug, replace 'console.log()' with 'alert()'.
console.log (oneChar + ' becomes --> ' + oneChar.replace (/\u0009/g, "") );
console.log (sixChars + ' becomes --> ' + sixChars.replace (/\\u0009/g, "") );
You need another escape .replace(/\\u009/g,''); :)

Categories

Resources