Escape / in .split() - javascript

Need to escape / in javascript .split() function but cannot seem to figure out!
input -> string "07777/25555,00255" or any of 0777/2555 0777,2555
output -> array {07777,25555,00255}
var p = item.gdp.split(/ , | \//);
Not really good with regex!

What this does is split on either " , " or " /" (note the space characters: space comma space and space forward slash). Your regular expression is absolutely fine if that's what you're intending to replace on.
Here's a Regexper visualisation:
Update
There are no spaces in your string at all, so you need to remove those:
item.gdp.split(/,|\//);
With this, your result will be:
["07777", "25555", "00255"]
A more practical regular expression to use though would be /[,\/] - the square brackets will match on any character held within them.

var item={gdp:"07777/25555,00255"};
var p = item.gdp.split(/[,/]/);
document.write(p[0] + "<br>" + p[1] + "<br>" + p[2]);
07777
25555
00255

Here's one
split(/\s*[,\/]\s*|\s+/);

If you are splitting on only comma and slash as in your first string
"07777/25555,00255"
you can simply split on the character class containing those two characters [,/]
Within a character class the slash does not need to be escaped, so the resulting statement would be
var p = item.gdp.split(/[,/]/);
If you also want to split on space, as in your other example 0777/2555 0777,2555 simply add space to the character class:
var p = item.gdp.split(/[, /]/);
or to split on any whitespace (space, tab, etc.) use the predefined \s:
var p = item.gdp.split(/[,\s/]/);
Further, you can collapse multiple whitespace, but then you need to go beyond a simple character class. Compare...
var str="07777/25555,00255 0777,3444";
// split on white, comma, or slash. multiple spaces causes multiple results
str.split(/[\s,/]/)
// -> ["07777", "25555", "00255", "", "", "", "", "0777", "3444"]
// split on multiple whitespace, OR on comma or slash
str.split(/\s+|[,/]/)
// -> ["07777", "25555", "00255", "0777", "3444"]

input.split(/[\/\s,]+/)
Is this what you are looking for?

Related

Escape single backslash inbetween non-backslash characters only

I have some input coming in a web page which I will re display and submit elsewhere. The current issue is that I want to double up all single backslashes that are sandwiched inbetween non-backslash characters before submitting the input elsewhere.
Test string "domain\name\\nonSingle\\\WontBe\\\\Returned", I want to only get the first single backslash, between domain and name.
This string should get nothing "\\get\\\nothing\\\\"
My current pattern that I can get closest with is [\w][\\](?!\\) however this will get the "\n" from the 1st test string i have listed. I would like to use lookbehind for the regex however javascript does not have such a thing for the version I am using. Here is the site I have been testing my regexs on http://www.regexpal.com/
Currently I am inefficiently using this regex [\w][\\](?!\\) to extract out all single backslashes sandwiched between non-backslash characters and the character before them (which I don't want) and then replacing it with the same string plus a backslash at the end of it.
For example given domain\name\\bl\\\ah my current regex [\w][\\]\(?!\\) will return "n\". This results in my code having to do some additional processing rather than just using replace.
I don't care about any double, triple or quadruple backslashes present, they can be left alone.
For example given domain\name\\bl\\\ah my current regex [\w][\\]\(?!\\) will return "n\". This results in my code having to do some additional processing rather than just using replace.
It will do just using replace, since you can insert the matched substring with $&, see:
console.log(String.raw`domain\name\\bl\\\ah`.replace(/\w\\(?!\\)/g, "$&\\"))
Easiest method of matching escapes, is to match all escaped characters.
\\(.)
And then in the replacement, decide what to do with it based on what was captured.
var s = "domain\\name\\\\backslashesInDoubleBackslashesWontBeReturned";
console.log('input:', s);
var r = s.replace(/\\(.)/g, function (match, capture1) {
return capture1 === '\\' ? match : '$' + capture1;
});
console.log('result:', r);
The closest you can get to actually matching the unescaped backslashes is
((?:^|[^\\])(?:\\\\)*)\\(?!\\)
It will match an odd number of backslashes, and capture all but the last one into capture group 1.
var re = /((?:^|[^\\])(?:\\\\)*)\\(?!\\)/g;
var s = "domain\\name\\\\escapedBackslashes\\\\\\test";
var parts = s.split(re);
console.dir(parts);
var cleaned = [];
for (var i = 1; i < parts.length; i += 2)
{
cleaned.push(parts[i-1] + parts[i]);
}
cleaned.push(parts[parts.length - 1]);
console.dir(cleaned);
The even-numbered (counting from zero) items will be unmatched text. The odd-numbered items will be the captured text.
Each captured text should be considered part of the preceding text.

Regex to not match when not in quotes

I'm looking to create a JS Regex that matches double spaces
([-!$%^&*()_+|~=`{}\[\]:";'<>?,.\w\/]\s\s[^\s])
The RegEx should match double spaces (not including the start or end of a line, when wrapped within quotes).
Any help on this would be greatly appreciated.
For example:
var x = 1,
Y = 2;
Would be fine where as
var x = 1;
would not (more than one space after the = sign.
Also if it was
console.log("I am some console output");
would be fine as it is within double quotes
This problem is a classic case of the technique explained in this question to "regex-match a pattern, excluding..."
We can solve it with a beautifully-simple regex:
(["']) \1|([ ]{2})
The left side of the alternation | matches complete ' ' and " ". We will ignore these matches. The right side matches and captures double spaces to Group 2, and we know they are the right ones because they were not matched by the expression on the left.
This program shows how to use the regex in JavaScript, where we will retrieve the Group 2 captures:
var the_captures = [];
var string = 'your_test_string'
var myregex = /(["']) \1|([ ]{2})/g;
var thematch = myregex.exec(string);
while (thematch != null) {
// add it to array of captures
the_captures.push(thematch[2]);
document.write(thematch[2],"<br />");
// match the next one
thematch = myregex.exec(string);
}
A Neat Variation for Perl and PCRE
In the original answer, I hadn't noticed that this was a JavaScript question (the tag was added later), so I had given this solution:
(["']) \1(*SKIP)(*FAIL)|[ ]{2}
Here, thanks to (*SKIP)(*FAIL) magic, we can directly match the spaces, without capture groups.
See demo.
Reference
How to match (or replace) a pattern except in situations s1, s2, s3...
Article about matching a pattern unless...
Simple solution:
/\s{2,}/
This matches all occurrences of one or more whitespace characters. If you need to match the entire line, but only if it contains two or more consecutive whitespace characters:
/^.*\s{2,}.*$/
If the whitespaces don't need to be consecutive:
/^(.*\s.*){2,}$/

javascript replace all occurrences ",\S" with ", \S"

I want to have spaces separating items in a CSV string. That is "123,456,789" => "123, 456, 789". I have tried, but been unable to construct a regexp to do this. I read some postings and thought this would to the trick, but no dice.
text = text.replace(new RegExp(",\S", "g"), ", ");
Could anyone show me what I am doing wrong?
You have two problems:
Backslashes are a pain in the, um, backslash; because they have so many meanings (e.g. to let you put a quote-mark inside a string), you often end up needing to escape the backslash with another backslash, so you need ",\\S" instead of just ",\S".
The \S matches a character other than whitespace, so that character gets removed and replaced along with the comma. The easiest way to deal with that is to "capture" it (by putting it in parentheses), and put it back in again in the replacement (with $1).
So what you end up with is this:
text = text.replace(new RegExp(',(\\S)', "g"), ", $1");
However, there is a slightly neater way of writing this, because JavaScript lets you write a regex without having a string, by putting it between slashes. Conveniently, this doesn't need the backslash to be escaped, so this much shorter version works just as well:
text = text.replace(/,(\S)/g, ", $1");
As an alternative to capturing, you can use a "zero-width lookahead", which in this situation basically means "this bit has to be in the string, but don't count it as part of the match I'm replacing". To do that, you use (?=something); in this case, it's the \S that you want to "look ahead to", so it would be (?=\S), giving us this version:
text = text.replace(/,(?=\S)/g, ", ");
There are 2 mistakes in your code:
\S in a string literal translates to just S, because \S is not a valid escape sequence. As such, your regex becomes /,S/g, which doesn't match anything in your example. You can escape the backslash (",\\S") or use a regex literal (/,\S/g).
After this correction, you will replace the character following the comma with a space. For instance, 123,456,789 becomes 123, 56, 89. There are two ways to fix this:
Capture the non-space character and use it in the replacement expression:
text = text.replace(/,(\S)/g, ', $1')
Use a negative lookahead assertion (note: this also matches a comma at the end of the string):
text = text.replace(/,(?!\s)/g, ', ')
text = text.replace(/,(\S)/g, ', $1');
try this:
var x = "123,456,789";
x = x.replace(new RegExp (",", "gi"), ", ");

Remove empty values from comma separated string javascript

How do I remove empty values from an comma separated string in JavaScript/jQuery?
Is there a straightforward way, or do I need to loop through it and remove them manually?
Is there a way to merge all the splits (str and str1) in JavaScript/jQuery?
CODE:
var str = '+ a + "|" + b';
var str1 = '+ a + "-" + b';
str = str.split("+").join(",").split('"|"').join(",");
str1 = str1.split("+").join(",").split('"-"').join(",");
console.log(str); //, a , , , b
console.log(str1); //, a , , , b
EXPECTED OUTPUT :
a,b
Help would be appreciated :)
As I see it, you want to remove +, "|", "-" and whitespace from the beginning and end of the string, and want to replace those within the string with a single comma. Here's three regexes to do that:
str = str.replace(/^(?:[\s+]|"[|-]")+/, '')
.replace(/(?:[\s+]|"[|-]")+$/, '')
.replace(/(?:[\s+]|"[|-]")+/g, ',');
The (?:[\s+]|"[|-]") matches whitespace or pluses, or "|" or "-". The + at the end repeats it one or more times. In the first expression we anchor the match to the beginning of the string and replace it with nothing (i.e. remove it). In the second expression we anchor the match to the end of the string and remove it. And in the third, there is no anchor, because all matches that are left have to be somewhere inside the string - and we replace those with ,. Note the g modifier for the last expression - without it only the first match would be replaced.
The other answer is useful, and may be exactly what you are looking for.
If, for some reason, you still want to use split, luckily that method takes a regex as separator, too:
str = str.split(/\s*\+\s*(?:"\|"\s*\+\s*)?/).slice(1).join(",");
str1 = str1.split(/\s*\+\s*(?:"-"\s*\+\s*)?/).slice(1).join(",");
Because you have a plus sign in front of the "a", you can slice the array to return only the elements after it.
Also, since you mentioned you were new to regular expressions, here is the explanation:
any amount of space
a plus sign
any amount of space
optional (because of the ? after the group, which is the parentheses): a non-capturing (that is what the ?: means) group containing:
"|"
any amount of space
another plus sign
any amount of space
Works perfectly fine:
str.split(/[ ,]+/).filter(function(v){return v!==''}).join(',')

Split string by regex in JavaScript

I'm trying to split a string by the following array of characters:
"!", "%", "$", "#"
I thought about using regex, so I developed the following method which I thought would split the string by the characters:
var splitted = string.split(/\!|%|\$|#*/);
However, when I run the following code, the output is split by every character, not what I was hoping for:
var toSplit = "abc%123!def$456#ghi";
var splittedArray = toSplit.split(/\!|%|\$|#*/);
How could I make it so that splittedArray contains the following elements?
"abc", "123", "def", "456", "ghi"
Any help appreciated.
#* matches the empty string and there's an empty string between any two characters, so the string is split at every single character. Use + instead:
/\!|%|\$|#+/
Also if you meant the + to apply to every character and not just # then group them up:
/(\!|%|\$|#)+/
Or better yet, use a character class. This lets you omit the backslashes since none of these characters are special inside square brackets.
/[!%$#]+/
Use the following:
var splittedArray = toSplit.split(/[!%$#]+/);
Your current code will split between every character because #* will match empty strings. I am assuming since you used #* that you want to consider consecutive characters a single delimiter, which is why the + is at the end of the regex. This will only match one or more characters, so it will not match empty strings.
The [...] syntax is a character class, which is like alternation with the | character except that it only works for single characters, so [!%$#] will match either !, %, $, or #. Inside of the character class the escaping rules change a little bit, so you can just use $ instead of \$.

Categories

Resources