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
Related
I want to replace each letter in more one word like this example:
if user typed #(*#$^ $^* by the code I want to change each letter to cinema man. Explaining more - I want to make a map like this:
"#" = "c";
"(" = "i";
"*" = "n";
"#" = "e";
"$" = "m";
"^" = "a";
How I can make that process with JavaScript?
If you're just wanting to map those specific characters, you can do something like this:
var myMap = {
"#": "c",
"(": "i",
"*": "n"
};
var string = "#(*";
var newLetters = string.split('').map(function(letter){
return myMap[letter];
});
newLetters.join('');
You just need to create an object to reference it's key-value pair.
Edit: Obviously you can fix this to work to your liking, just something I whipped up to illustrate the over all idea.
Create a object in order to store the mapping. Now parse the string and replace each word with its mapped work. Here is a working demo:
var mapping = {
"#": "c",
"(": "i",
"*": "n",
"#": "e",
"$": "m",
"^": "a"
};
var string = "#(*#$^ $^*";
var output = string.split("").map(function(el) {
if(mapping.hasOwnProperty(el))
return mapping[el];
return el;
}).join("");
alert(output);
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? :)
I have the function below that selects randomly letters from an array of all English letters (plus space, plus <br/>) and stores them in a string variable.
The function has a loop that generates 2,500 random characters. I would like to check which of the sequences of letters between two spaces (i.e. " " or a space and a return (" " or <br/>) or two <br/> constitute legitimate English words.
How do I do this? In particular,
do I need to download an English dictionary
how do I compare all of my strings of characters against it
retain the words that are legitimate? Here is the code of the
function.
JS
function statement() {
var letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", " ", "<br/>"];
for (var i = 0; i <= 2500; i++) {
var random_letter = Math.floor(Math.random() * letters.length);
result += letters[random_letter];
}
document.getElementById("random1").innerHTML += result;
}
At first: you need a wordlist. If you work with a Unix OS you most probably have a medium sized wordlist in /usr/share/dict/words but you'll find many on the net, one short search gave http://www-01.sil.org/linguistics/wordlists/english/wordlist/wordsEn.txt with about 100k words. These are already sorted so you can just put them in an Array word by word. Because it is sorted you can search it with a fast binary search.
You look for words between spaces and linebreaks but it is a bit ambiguous if e.g. word<br/>word2 word3<br/> counts as word, word2, word3 or word, word2 word3 so word2 word3 would be a kind of sentence? I assume the first variation: single words, no sentences from now on for simplicity.
In the loop that produces the string, I would check every single generated character if it is space/linebreak, take the string generated so far and check that against the dictionary.
If you insist to work on the final generated string only you have to split it into single words, maybe with a regex because it is simple here:
var s = ("word1 word2<br/> <br/><br/> word3 <br/>").replace(/<br[/]>/g,"").split(/[\s]+/);
s.join(",");
//word1,word2,word3,
// the ^ last one is empty
Then loop over the array and check every word you found against the dictionary. Because the last entry in my poor version of splitting is empty and others might be empty, too (if you played around with it), you need to check every one if it is has something in it.
Instead of doing the dictionary search with a simple array and a binary search you can leave that hassle to the JavaScript engine by (ab)using an {key:value} object. (this example is sorted because of C&P from an already sorted wordlist, it does not needed to be sorted)
dict = {Banach:0,Bancroft:0,Bandung:0,"Banga lore":0,Bangkok:0,Bangladesh:0,
Bangladeshi:0,Bangor:0,Bangui:0,Banjarmasin:0,Banjul:0,Banks:0,
Banneker:0,Deleon:0,Delgado:0,Delhi:0,Delia:0,Delibes:0,Delicious:0,
Delilah:0,Delius:0,Dell:0,Dell:0,Della:0,Delmar:0,Delmarva:0,Delmer:0,
Delmonico:0,Delores:0,Deloris:0,Hanukkahs:0,Hapsburg:0,Harare:0,
Harbin:0,Hardin:0,Harding:0,Hardy:0,Hargreaves:0,Harlan:0,Harlem:0,
Harlequin:0,Harley:0,Harlow:0,Harmon:0};
dict.hasOwnProperty("Delores"); // true
dict.hasOwnProperty("foo"); // false
You use the key as the value here. The actual value is wasted in this case but you can use them for further refinement, e.g.: mark nouns, verbs, adjectives, etc. You are not restricted to numbers, of course, you can use everything, even the complete dictionary entry of that word (with pictures and music even, but that is another story).
this is working code implemented with node js
1) Yes, you do have to download the words, I get them from a url using the request module.
You basically have to turn the list of words from that website into an array by splitting it with a regualr expression, and also split your random generated strings with another regex. Then, you do a for loop to go through each of them, one at a time, comparing each real word to each generated word. Like this:
var request = require("request");
request("http://www.math.sjsu.edu/~foster/dictionary.txt", function(err,status, resp){
var letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", " ", "<br/>"];
letters = letters.map(function(letter){ return letter.toLowerCase()});
//console.log(wordsArr);
var result = "";
var c = 0;
for (var i = 0; i <= 10000; i++) {
var random_letter = Math.floor(Math.random() * letters.length);
result += letters[random_letter];
c++;
}
if(c >= 2500){
var randomResultArr = result.split(/<br\/>|<br\/><br\/>|\s/);
console.log(randomResultArr);
var matchesArr = getWordMatches(wordsArr = resp.split("\n"), result.split(/<br\/>|<br\/><br\/>|\s/));
console.log("MATCHES: " + matchesArr);
}
});
function getWordMatches(wordsArr, resultArr){
var matchesArr = [];
console.log("WORDSARR ", wordsArr.length);
console.log("RES ARR ", resultArr.length);
for(var i = 0; i < wordsArr.length; i++ ){
for(var x = 0; x < resultArr.length; x++){
if( (wordsArr[i] === resultArr[x]) && wordsArr[i] !== "" ){
matchesArr.push(wordsArr[i]);
console.log("WORD MATCH : " + wordsArr[i]);
}
}
}
return matchesArr;
}
here is a sample output of it, I had to up the 2500 to 10000 to get more matches:
WORDSARR 349901
RES ARR 747
WORD MATCH : giros
WORD MATCH : goad
WORD MATCH : kaqa
WORD MATCH : lome
WORD MATCH : mibs
MATCHES: giros,goad,kaqa,lome,mibs
Please consider this scenario:
I have a Key/Value variable like this:
var dict = {"78": "X",
"12": "G",
"18": "R",
"67": "U",
"68": "O",
"30": "P"}
I have a string that I want to check if there is a letter of my variable exist in my string. How I can do this?
Collect object key values and use them to create a regular expression. Then use .test(yourString):
var myStr = "Group";
new RegExp(Object.keys(dict).map(function (c) { return dict[c]; }).join("|"))
.test(myStr); // true
Another way is simply iterating the object:
var found = false;
for (var k in dict) {
if (myStr.indexOf(dict[k]) !== -1) {
found = true;
break;
}
}
if (found) {
// letter found
}
One possible approach:
var contains = Object.keys(dict).some(function(key) {
return someStr.indexOf(dict[key]) !== -1;
});
In other words, iterate over the collection, at each step checking whether or not the tested character present in your string (someStr). If it is, stops the iteration immediately (that's how Array.prototype.some works).
Another approach is building a character class regex, then using it against the string:
var pattern = RegExp('[' + Object.keys(dict).map(function(k) {
return dict[k];
}).join('') + ']');
var contains = pattern.test(someStr);
The second approach is slightly better if the tested strings usually do contain the characters from dict. It's also quite easy to augment this solution into case-insensitive search - just add 'i' string as a second param of RegExp call.
The caveat is that you'll have to escape the characters that will be considered meta within a string passed into RegExp constructor (backslash, for example). If there are no such characters in the dictionary, it's not a problem, though.
It can be done using the following:
str = "test";
isExists = false;
for(var key in dict){
isExists = str.indexOf(dict[key]) !== -1;
if(isExists)
break;
};
console.log(isExists);
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".