Convert \n\r to string does nothing in jquery - javascript

I want to take a text from my textarea, put in a variable, an change all the linebreaks (\n\r) to "##".
For some reason it won't work.
Help please, here's a fiddle: http://jsfiddle.net/HK82q/
$("#go").click(function(){
curtext = $("textarea").val();
curtext = curtext.replace("\n\r\n","##");
alert(curtext);
});

"Line break" can mean one of three things:
\r (carriage return), used by old Mac computers
\n (line feed), used by Linux, Unix, and I think new Macs
\r\n (CRLF), used by Windows
Therefore, you need to handle all three cases. This can be done with multiple .replace calls, or a regex:
curtext = curtext.replace(/(?=[\r\n])\r?\n?/g,"##");
This regex works by first asserting that there is either a CR or LF ahead, then matching them optionally to allow for all three options. The assertion ensures that "nothing" doesn't match.

You're expecting to meet a \n followed by \r. It's wrong. You should expect either one or the other. Then, regex for replace function should be enclosed with / not with ". It's not a string. The last thing - add the g modifier (stands for global - will replace all occurences, not only the first one).
curtext = curtext.replace(/[\n\r]/g,"##");
Here's the updated fiddle: http://jsfiddle.net/HK82q/1/

Related

Why Javascript string.replace("\n\t","xxx") replaces "\n\t" with "\nxxx"?

I expect to replace "\n\t" with "xxx" in a txt file:
"数字多功能光盘 DVD shùzì"
I do this: str.replace("\n\t","xxx")
method matches needed parts but leaves \n part and only replaces \t for 'xxx'.WHY?
why when use crtl+F in VSCOde and it works like charm but in code it doesn't.
First of all, str.replace("a","b") only replaces the first occurrence in JavaScript. To replace all of them, you need to use a regex with g modifier. So, you could try str.replace(/\n\t/g,"xxx") first.
Next, why does it work in VSCode? In VSCode regex, \n matches any line break sequence that is selected in the bottom right-hand corner of VSCode app. It works as \R in PCRE, Java, Onigmo, etc. in this case.
As there can be many line ending sequences you may consider "converting" VSCode \n to (?:\r\n|[\r\n\x0B\x0C\x85\u2028\u2029]) that matches any single Unicode line break sequence and use
s = s.replace(/(?:\r\n|[\r\n\x0B\x0C\x85\u2028\u2029])\t/g, '')

Replacing carriage returns (?) after compiling HTML

After parsing HTML I get the following object:
I would like to strip all the "↵" except of one. How can I do this? I tried with something like this:
weirdString.replace(/(\r\n|\n|\r)/gm, ""));
However, this replaces all the "↵" but as I've already mentioned I want to replace all of those except the first...
You may capture it and restore with a backreference:
weirdString.replace(/^([^\S\r\n]*(?:\r\n?|\n))|(?:\r\n?|\n)/g, "$1"));
No need using m modifier here.
Details:
^ - start of a string
([^\S\r\n]*(?:\r\n?|\n)) - Capturing group 1:
[^\S\r\n]* - any 0+ whitespaces other than CR and LF
(?:\r\n?|\n) - any style line break
| - or
(?:\r\n?|\n) - any style line break.
With $1, only the contents captured into Group 1 are put back in the replacement result.
var weirdString = " \r\n\r\n\n\rSome text";
console.log(weirdString.replace(/^([^\S\r\n]*(?:\r\n?|\n))|(?:\r\n?|\n)/g, "$1"));
A little bit tricky, but why dont you first replace your first carriage return with something else? e.g.: %#% or something else, what your are not using in your text... then replace all other carriage returns, and at last return your %#% tag back to carrige return...
The exact matching regexp must cope with some things you have not accounted for:
first is whitespace that can be in between two such line ends. It should be considered the case of intervening.
Second is that the \r in front of \n should be considered optional, as it appears in texts that come from socket connections from internet (most protocols force to send \r\n but can be optional.
a sequence of two or more newlines of this type should be collapsed to one \n (or one \r\n as you prefer)
If you do a pattern match and substitute with multiple flag enabled you'll get the desired effect with this pattern:
([ \t]*\r*\n)+
as seen in the following demo. I have substituted the newlines by a [<--']\r\n to be able to see the effect. It also deletes all trailing whitespace at line ends (normally invisible) but doesn't touch the leading at beginning of lines (this could affect the visible looking of your text)

JavaScript regular expression replace - why does one work, but this other not?

I grabbed the following JavaScript regular expression replace from another site to strip out some invalid characters:
str = str.replace(/[^\u000D\u00B7\u0020-\u007E\u00A2-\u00A4]/g,'');
However, I noticed it wasn't catching occurrences of \00B7 (the ISO-8859-1 center dot character).
If I did it in two steps however, it works:
str = str.replace(/\u00B7/g,'');
str = str.replace(/[^\u000D\u00B7\u0020-\u007E\u00A2-\u00A4]/g,'');
The 1st replace seems to be included in the 2nd replace. Can somebody explain to me why the 2nd line doesn't work all by itself. Thanks.
The first and second pattern are completely different. Pattern one replaces \u00B7, while the second pattern replaces all characters NOT listed in the pattern. Remove the carat from pattern two and that should fix your issue.
Just to be clear:
/[^\u000D\u00B7\u0020-\u007E\u00A2-\u00A4]/
matches all characters not in the set. So to match \u00B7 (and have it replaced with ''), remove it from the pattern:
/[^\u000D\u0020-\u007E\u00A2-\u00A4]/
The ASCII character set is given at http://www.asciitable.com/, likely that is the set you want to keep. The range \u0020-\u007E covers most the common set that is of interest, the others are typically not wanted.
\u000D is a carriage return, I would investigate whether you really need u00A2, u00A3 and u00A4.

Regex: Not the beginning of a line?

I have a string like this: ----------- 243f,33f----
Now I want to remove all the - chars except the first -, the , and the numbers. The result should be -243,33. How would I do this?
Your question still isn't very clear, but this yields the output you want:
'----------- 243f,33f----'.replace(/(?!^-)[^\d,]+/g, '')
The regex matches one or more of any character except digits or commas, after making sure the first character isn't a hyphen at the beginning of the string.
EDIT: To those who came up with regexes using (?<!^) and then withdrew them because JavaScript doesn't support lookbehinds, that wasn't necessary. I know there were other problems with those answers, but for the purpose of matching "not the beginning of the string", (?!^) works just fine.
'----------- 243f,33f----'.replace(/[^-0123456789,]/g, '').replace(/^-/, 'x').replace(/-/g, '').replace(/x/, '-')
output: '-243,33'
replace the substring from second character ala
string.charAt(0) + string.substring(1).replace("-","");
but i don't know if the syntax is correct
EDIT:
oh if you want to remove the f's too you can remove any nondigit and non-comma:
string.charAt(0) + string.substring(1).replace("[^0-9,]","");
Try this one:
(?:(^-)|[^\d,\r\n])
and replace with
$1
See it here on Regexr
Its a bit more difficult since Javascript does not support look behinds. So if there is a - at the start of the line it will be replaced by itself, for the other matches $1 is empty so replaced with nothing.

Regex to check only capital letters, two special characters (& and Ñ) & without any space between

I am using below code snippet to validate my input string with: only capital letters, numbers and two special characters (those are & and Ñ) & without any space between.
var validpattern = new RegExp('[^A-Z0-9\d&Ñ]');
if (enteredID.match(validpattern))
isvalidChars = true;
else
isvalidChars = false;
Test 1: "XAXX0101%&&$#" should fail i.e isvalidChars = false; (as it contains invalid characters like %$#.
Test 2: "XAXX0101&Ñ3Ñ&" should pass.
Test 3: "XA 87B" should fail as it contains space in between
The above code is not working, Can any one help me rectifying the above regex.
This is happening because you have a negation(^) inside the character class.
What you want is: ^[A-Z0-9&Ñ]+$ or ^[A-Z\d&Ñ]+$
Changes made:
[0-9] is same as \d. So use
either of them, not both, although it's not incorrect to use both, it's redundant.
Added start anchor (^) and end
anchor($) to match the entire
string not part of it.
Added a quantifier +, as the
character class matches a single
character.
^[A-Z\d&Ñ]+$
0-9 not required.
if you want valid patterns, then you should remove the ^ in the character range.
[A-Z0-9\d&Ñ]
Using jquery we could achieve the same in one line:
$('#txtId').alphanumeric({ allow: " &Ñ" });
Using regex (as pointed by codaddict) we can achieve the same by
var validpattern = new RegExp('^[A-Z0-9&Ñ]+$');
Thanks everyone for the precious response added.

Categories

Resources