Split person[0].email into ['person', '0', 'email'] - javascript

I don't know if this has been asked before, because English is not my first language and I don't know the keywords to search.
So basically I have the following input element,
<input type="email" name="person[0].email" />
I would like to split the name into 3 parts like ["person", "0", "email"].
I have tried using /(\[[^[\]]])|\./ but it gives ["person", "[0]", "", undefined, "email"].
Also, for a[0][1].b[3].c, it should output ["a", "0", "1", "b", "3", "c"]

You can use .match instead of .split
console.log("person[0].email".match(/\w+/g));
Note (thanks #npinti): in case if in name will be _ my first example will match also _, so in this case you need just use regexp like this
console.log("person[0].email".match(/[A-Za-z0-9]+/g));

Seems like you want to break on anything which is not a letter or a number, thus you could use this: [^A-Za-z0-9]+. An example of the expression is available here.

You can use this split:
var m = person[0].email".split(/[.\[\]]+/g)
//=> ["person", "0", "email"]
OR:
m = "a[0][1].b[3].c".split(/[.\[\]]+/g)
//=> ["a", "0", "1", "b", "3", "c"]

If you always have the following format: name[i].prop, you can do this without regular expressions:
function splitName(s)
{
var parts1 = s.split('].');
var parts2 = parts1[0].split('[');
return [parts2[0], parts2[1], parts1[1]];
}
document.body.innerHTML += JSON.stringify(splitName('name[i].prop')) + '<br/>';
document.body.innerHTML += JSON.stringify(splitName('person[0].email')) + '<br/>';
document.body.innerHTML += JSON.stringify(splitName('a[0].b')) + '<br/>';
It is less elegant, not universal and works only for the specified format. However, there are no regular expressions and if you work only with this format - then why not? :)

Related

regex capturing with repeating pattern

I'm trying to capture all parts of a string, but I can't seem to get it right.
The string has this structure: 1+22+33. Numbers with an operator in between. There could be any number of terms.
What I want is ["1+22+33", "1", "+", "22", "+", "33"]
But I get: ["1+22+33", "22", "+", "33"]
I've tried all kinds of regexes, this is the best I've got, but it's obviously wrong.
let re = /(?:(\d+)([+]+))+(\d+)/g;
let s = '1+22+33';
let m;
while (m = re.exec(s))
console.log(m);
Note: the operators may vary. So in reality I'd look for [+/*-].
You can simply use String#split, like this:
const input = '3+8 - 12'; // I've willingly added some random spaces
console.log(input.split(/\s*(\+|-)\s*/)); // Add as many other operators as needed
Just thought of a solution: /(\d+)|([+*/-]+)/g;
You only have to split on digits:
console.log(
"1+22+33".split(/(\d+)/).filter(Boolean)
);

Split by Caps in Javascript

I am trying to split up a string by caps using Javascript,
Examples of what Im trying to do:
"HiMyNameIsBob" -> "Hi My Name Is Bob"
"GreetingsFriends" -> "Greetings Friends"
I am aware of the str.split() method, however I am not sure how to make this function work with capital letters.
I've tried:
str.split("(?=\\p{Upper})")
Unfortunately that doesn't work,
any help would be great.
Use RegExp-literals, a look-ahead and [A-Z]:
console.log(
// -> "Hi My Name Is Bob"
window.prompt('input string:', "HiMyNameIsBob").split(/(?=[A-Z])/).join(" ")
)
You can use String.match to split it.
"HiMyNameIsBob".match(/[A-Z]*[^A-Z]+/g)
// output
// ["Hi", "My", "Name", "Is", "Bob"]
If you have lowercase letters at the beginning it can split that too. If you dont want this behavior just use + instead of * in the pattern.
"helloHiMyNameIsBob".match(/[A-Z]*[^A-Z]+/g)
// Output
["hello", "Hi", "My", "Name", "Is", "Bob"]
To expand on Rob W's answer.
This takes care of any sentences with abbreviations by checking for preceding lower case characters by adding [a-z], therefore, it doesn't spilt any upper case strings.
// Enter your code description here
var str = "THISSentenceHasSomeFunkyStuffGoingOn. ABBREVIATIONSAlsoWork.".split(/(?=[A-Z][a-z])/).join(" "); // -> "THIS Sentence Has Some Funky Stuff Going On. ABBREVIATIONS Also Work."
console.log(str);
The solution for a text which starts from the small letter -
let value = "getMeSomeText";
let newStr = '';
for (var i = 0; i < value.length; i++) {
if (value.charAt(i) === value.charAt(i).toUpperCase()) {
newStr = newStr + ' ' + value.charAt(i)
} else {
(i == 0) ? (newStr += value.charAt(i).toUpperCase()) : (newStr += value.charAt(i));
}
}
return newStr;

Regex split on upper case and first digit

I need to split the string "thisIs12MyString" to an array looking like [ "this", "Is", "12", "My", "String" ]
I've got so far as to "thisIs12MyString".split(/(?=[A-Z0-9])/) but it splits on each digit and gives the array [ "this", "Is", "1", "2", "My", "String" ]
So in words I need to split the string on upper case letter and digits that does not have an another digit in front of it.
Are you looking for this?
"thisIs12MyString".match(/[A-Z]?[a-z]+|[0-9]+/g)
returns
["this", "Is", "12", "My", "String"]
As I said in my comment, my approach would be to insert a special character before each sequence of digits first, as a marker:
"thisIs12MyString".replace(/\d+/g, '~$&').split(/(?=[A-Z])|~/)
where ~ could be any other character, preferably a non-printable one (e.g. a control character), as it is unlikely to appear "naturally" in a string.
In that case, you could even insert the marker before each capital letter as well, and omit the lookahead, making the split very easy:
"thisIs12MyString".replace(/\d+|[A-Z]/g, '~$&').split('~')
It might or might not perform better.
In my rhino console,
js> "thisIs12MyString".replace(/([A-Z]|\d+)/g, function(x){return " "+x;}).split(/ /);
this,Is,12,My,String
another one,
js> "thisIs12MyString".split(/(?:([A-Z]+[a-z]+))/g).filter(function(a){return a;});
this,Is,12,My,String
You can fix the JS missing of lookbehinds working on the array split using your current regex.
Quick pseudo code:
var result = [];
var digitsFlag = false;
"thisIs12MyString".split(/(?=[A-Z0-9])/).forEach(function(word) {
if (isSingleDigit(word)) {
if (!digitsFlag) {
result.push(word);
} else {
result[result.length - 1] += word;
}
digitsFlag = true;
} else {
result.push(word);
digitsFlag = false;
}
});
I can't think of any ways to achieve this with a RegEx.
I think you will need to do it in code.
Please check the URL, same question different language (ruby) ->
The code is at the bottom:
http://code.activestate.com/recipes/440698-split-string-on-capitalizeduppercase-char/

How to split words using javascript

This might be a simple question but, how do i split words... for example
a = "even, test"
I have used .split to seperate the text with space.
so the result came is like
a = "even,"
b = "test"
But, how do I remove the 'comma' here?
But in some conditions it might get "even test" and in some conditions i might get "even, test". All are dynamic, so how do i check it for both?
Thanks
Firstly, the split() function is not jQuery - it is pure Javascript.
Did you try doing split with a comma and a space? That would have worked just fine in your case:
var result = input.split(', ');
For more complex splits, you can use regular expression pattern matching to allow multiple commas or spaces between the two fields:
var result = input.split(/[, ]+/);
but you probably don't need to go that far in your case.
I think is better to use something like this:
text.match(/[a-z'\-]+/gi);
Example:
var e=function()
{
var r=document.getElementById('r');
var x=document.getElementById('t').value.match(/[a-z'\-]+/gi);
for(var i=0;i<x.length;i++)
{
var li=document.createElement('li');
li.innerText=x[i];
r.appendChild(li);
}
}
<div style="float:right;width:18%">
<ol id="r" style="display:block;width:auto;border:1px inner;overflow:scroll;height:8em;max-height:10em;"></ol>
<button onclick="e()">Extract words!</button>
</div>
<textarea id="t" style="width:70%;height:12em">even, test; spider-man
But saying o'er what I have said before:
My child is yet a stranger in the world;
She hath not seen the change of fourteen years,
Let two more summers wither in their pride,
Ere we may think her ripe to be a bride.
—Shakespeare, William. The Tragedy of Romeo and Juliet</textarea>
I found a list of word separators in Sublime Text default settings.
Here's how to split with it, with some Unicode support (the defined separators are not Unicode though):
{ // word_separators: ./\()"'-,;<>~!##$%^&*|+=[]{}`~?: (32)
function splitByWords(str = '', limit = undefined) {
return str.split(/[-./\\()"',;<>~!##$%^&*|+=[\]{}`~?:]/u, limit)
}
function reverseString(str) {
let newString = ''
for (let i = str.length - 1; i >= 0; i--)
newString += str[i]
return newString
}
const str = '123.x/x\\x(x)x"x\'x-x:x,789;x<x>x~x!x#x#x$x%x^x&x*x|x+x=x[x]x{x}x`x~x?456'
console.log(splitByWords(str)) // (33) ["123", "x", "x", "x", "x", "x", "x", "x", "x", "x", "789", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "456"]
console.log(splitByWords(str, 1)) // ["123"]
console.log(splitByWords(reverseString(str), 1)) // ["654"]
}
For some reason the - has to be at the beginning, and the : at the end.
Edit: you might want to add \s (after the -) to count whitespace as separator
Just use this code:
var a = "even, test";
var words = a.split(", ");
a.split(',')
or
var re = /\s*,\s*/
var newA = a.split(re);
I think you could do it like this:
var a= 'even,'
var newA = a.slice(0, -1)
This will remove the last char from a given string.
And to check if the string contains a comma, I would do the following:
if (a.indexOf(",") >= 0){
//contains a comma
} else {
//no comma
}
I am a javascript beginner, so this probably is not the best way to do it, but nevertheless it works.
Hej Harry
if the comma is the separator you can call split with the comma
Ex:
var newColls = myString.split(",");
and not split with space.
GL

Javascript Match and RegExp Issue -- Strange Behavior

I have been trying to use a simple jQuery operation to dynamically match and store all anchor tags and their texts on the page. But I have found a weird behavior. When you are using match() or exec(), if you designate the needle as a separate RegExp object or a pattern variable, then your query matches only one instance among dozens in the haystack.
And if you designate the pattern like this
match(/needle/gi)
then it matches every instance of the needle.
Here is my code.
You can even fire up Firebug and try this code right here on this page.
var a = {'text':'','parent':[]};
$("a").each(function(i,n) {
var module = $.trim($(n).text());
a.text += module.toLowerCase() + ',' + i + ',';
a.parent.push($(n).parent().parent());
});
var stringLowerCase = 'b';
var regex = new RegExp(stringLowerCase, "gi");
//console.log(a.text);
console.log("regex 1: ", regex.exec(a.text));
var regex2 = "/" + stringLowerCase + "/";
console.log("regex 2: ", a.text.match(regex2));
console.log("regex 3: ", a.text.match(/b/gi));
For me it is returning:
regex 1: ["b"]
regex 2: null
regex 3: ["b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b"]
Can anyone explain the root of this behavior?
EDIT: I forgot to mention that for regex1, it doesn't make any difference whether you add the flags "gi" for global and case insensitive matching. It still returns only one match.
EDIT2: SOlved my own problem. I still don't know why one regex1 matches only one instance, but I managed to match all instances using the match() and the regex1.
So..this matches all and dynamically!
var regex = new RegExp(stringLowerCase, "gi");
console.log("regex 2: ", a.text.match(regex));
This is not unusual behaviour at all. In regex 1 you are only checking for 1 instance of it where in regex 3 you have told it to return all instances of the item by using the /gi argument.
In Regex 2 you are assuming that "/b/" === /b/ when it doesn't. "/b/" !== /b/. "/b/" is a string that is searching so if you string has "/b/" in it then it will return while /b/ means that it needs to search between the slashes so you could have "abc" and it will return "b"
I hope that helps.
EDIT:
Looking into it a little bit more, the exec methods returns the first match that it finds rather than all the matches that it finds.
EDIT:
var myRe = /ab*/g;
var str = "abbcdefabh";
var myArray;
while ((myArray = myRe.exec(str)) != null)
{
var msg = "Found " + myArray[0] + ". ";
msg += "Next match starts at " + myRe.lastIndex;
console.log(msg);
}
Having a look at it again it definitely does return the first instance that it finds. If you looped through it then would return more.
Why it does this? I have no idea...my JavaScript Kung Fu clearly isnt strong enough to answer that part
The reason regex 2 is returning null is that you're passing "/b/" as the pattern parameter, while "b" is actually the only thing that is actually part of the pattern. The slashes are shorthand for regex, just as [ ] is for array. So if you were to replace that to just new regex("b"), you'd get one match, but only one, since you're omitting the "global+ignorecase" flags in that example. To get the same results for #2 and #3, modify accordingly:
var regex2 = stringLowerCase;
console.log("regex 2: ", a.text.match(regex2, "gi"));
console.log("regex 3: ", a.text.match(/b/gi));
regex2 is a string, not a RegExp, I had trouble too using this kind of syntax, tho i'm not really sure of the behavior.
Edit : Remebered : for regex2, JS looks for "/b/" as a needle, not "b".

Categories

Resources