I am trying to find a regular expression to find the occurence of certain sequence of characters inside a set of words..
Lets say if I have a set of words
["Microsoft Windows", "International Superstar", "algorithm" , "stegration stunt"]
I am trying to find the words in which character "in" has occured.. so it should return the words "Microsoft Windows", "International Superstar"
Ive tried var match = /(in)/i.exec("Microsoft Windows")
it dint do the trick.. the sequnce "in" is just an example.. it could be anyset of characters..
You can use this regex
/".*in.*"/gi
you can try this
First define your regex,
var testRegex = 'in';
then create your regex object
var myRegex = new RegExp(testRegex,"g");
then go ahead and use it like any normal regex.
note: it's case-sensitive.
<script>
var arr = ["Microsoft Windows", "International Superstar", "algorithm" , "stegration stunt"];
var i;
var res = [];
for (i=0;i<arr.length;i++) {
if(arr[i].indexOf('in') > -1){
res.push(arr[i]);
}
}
alert(res);
</script>
Related
I have a string with keywords, separated by comma's.
Now I also have a nice RegEx, to filter out all the keywords in that string, that matches a queried-string.
Check out this initial question - RegEx - Extract words that contains a substring, from a comma seperated string
The example below works fine; it has a masterString, and a resultString. That last one only contains the keywords that has at least the word "car" in it.
masterString = "typography,caret,car,align,shopping-cart,adjust,card";
resultString = masterString.match(/[^,]*car[^,]*/g);
console.log(resultString);
Result from the code above;
"caret", "car", "shopping-cart", "card"
But how can I use the RegEx, with a variable matching-word (the word "car" in this example static and not variable).
I think it has to do something with a RegExp - but I can't figure out...
Here's a general solution for use with regexes:
var query = "anything";
// Escape the metacharacters that may be found in the query
// sadly, JS lacks a built-in regex escape function
query = query.replace(/[-\\()\[\]{}^$*+.?|]/g, '\\$&');
var regex = new RegExp("someRegexA" + query + "someRegexB", "g");
As long as someRegexA and someRegexB form a valid regex with a literal in-between, the regex variable will always hold a valid regex.
But, in your particular case, I'd simply do this:
var query = "car";
var items = masterString.split(",");
query = query.toLowerCase();
for (var i = 0; i < items.length; ++i) {
if (items[i].toLowerCase().indexOf(query) >= 0) {
console.log(items[i]);
}
}
How about this one?, you only need to replace \ \ with String , and it works for me. it can find whether your string has "car", not other similar word
var query = 'car';
var string = "car,bike,carrot,plane,card";
var strRegEx = '[^,]*'+query+'[,$]*';
string.match(strRegEx);
Answer provided by OP and removed from inside the question
I figured out this quick-and-maybe-very-dirty solution...
var query = 'car';
var string = "car,bike,carrot,plane,card";
var regex = new RegExp("[^,]*|QUERY|[^,]*".replace('|QUERY|',query),'ig');
string.match(regex);
This code outputs the following, not sure if it is good crafted, 'though..
"car", "carrot", "card"
But ended figuring out another, much simpler solution;
var query = "car";
var string = "car,bike,carrot,plane,card";
string.match(new RegExp("[^,]*"+query+"[^,]*",'ig'));
This code outputs the string below;
["car", "carrot", "card"]
My app-search-engine now works perfect :)
I have a string with this format:
#someID#tn#company#somethingNew#classing#somethingElse#With
There might be unlimited #-separated words, but definitely the whole string begins with #
I have written the following regexp, though it matches it, but I cannot get each #-separated word, and what I get is the last recursion and the first (as well as the whole string). How can I get an array of every word in an element separately?
(?:^\#\w*)(?:(\#\w*)+) //I know I have ruled out second capturing group with ?: , though doesn't make much difference.
And here is my Javascript code:
var reg = /(?:^\#\w*)(?:(\#\w*)+)/g;
var x = null;
while(x = reg.exec("#someID#tn#company#somethingNew#classing#somethingElse#With"))
{
console.log(x);
}
And here is the result (Firebug, console):
["#someID#tn#company#somet...sing#somethingElse#With", "#With"]
0
"#someID#tn#company#somet...sing#somethingElse#With"
1
"#With"
index
0
input
"#someID#tn#company#somet...sing#somethingElse#With"
EDIT :
I want an output like this with regular expression if possible:
["#someID", "#tn", #company", "#somethingNew", "#classing", "#somethingElse", "#With"]
NOTE that I want a RegExp solution. I know about String.split() and String operations.
You can use:
var s = '#someID#tn#company#somethingNew#classing#somethingElse#With'
if (s.substr(0, 1) == "#")
tok = s.substr(1).split('#');
//=> ["someID", "tn", "company", "somethingNew", "classing", "somethingElse", "With"]
You could try this regex also,
((?:#|#)\w+)
DEMO
Explanation:
() Capturing groups. Anything inside this capturing group would be captured.
(?:) It just matches the strings but won't capture anything.
#|# Literal # or # symbol.
\w+ Followed by one or more word characters.
OR
> "#someID#tn#company#somethingNew#classing#somethingElse#With".split(/\b(?=#|#)/g);
[ '#someID',
'#tn',
'#company',
'#somethingNew',
'#classing',
'#somethingElse',
'#With' ]
It will be easier without regExp:
var str = "#someID#tn#company#somethingNew#classing#somethingElse#With";
var strSplit = str.split("#");
for(var i = 1; i < strSplit.length; i++) {
strSplit[i] = "#" + strSplit[i];
}
console.log(strSplit);
// ["#someID", "#tn", "#company", "#somethingNew", "#classing", "#somethingElse", "#With"]
I created a function that should generate a random paragraph, but I would like to get some advice on how can I display the number of times each word in the paragraph is used, once the button is clicked.... do you use something like a counter variable?
<html>
<head>
<title></title>
<script type="text/javascript">
<!--
var article = ["the", "be", "let", "done", "any"];
var noun = ["boy", "girl", "dog", "town", "car"];
var verb = ["drove","jumped", "ran", "walked", "skipped"];
Use regular expression match, and get the length of the result for each searched word.
For example:
var countThe = sentence.match(/the/g).length;
update: More generally:
function countOccurances(sentence, word){
var reg = RegExp(word,'g');
return sentence.match(reg).length
}
With a test:
var sentence = "This is the sentence that we are going to look in for testing -- not the real thing." ;
alert(countOccurances(sentence, "the"))
This will count the frequency of all words in a string.
function countWords(s) {
var a = s.match(/\w+/g);
var counts = {};
for(var i = 0; i < a.length; i++) {
var elem = a[i];
if(!counts[elem]) counts[elem] = 1;
else counts[elem]++;
}
return counts;
}
You can use javascript string.match is probably the easiest method.
function checkFor(wordsArray,inString){
returnObject={};
for(var i=0;i<wordsArray.length;i++){
returnObject[wordsArray[i]]=inString.match(new RegExp("([^A-Za-z]|^)"+wordsArray[i]+"([^A-Za-z]|$)","gi")).length
}
return returnObject;
}
checkFor(["hi","peter"],"I would like to say to you Peter, hi howdy hello, I think hi is the best of these greetings for you");
Returns Object {hi: 2, peter: 1};
Then just run this on each of the arrays with the sentance as the inString argument
EDIT:
To explain what this does,
It is a function where you can pass in an array of words to look for and it will output an associative array or object (JavaScript doesn't distinguish between the two) of the counts of those values
The function loops through each element in the words array
It then runs the regular expression /([^A-Za-z]|^)wordGoesHere([^A-Za-z]|$)/gi which is a bit complicated I admit
the regular expression checks for "WordGoesHere" and makes sure that is is a word not just part of another word, the ([^A-Za-z]|^) checks that the character before the word is not in the set A-Z or is the start of the string so basically allows spaces, commas, full stops and any other punctual thing you can imagine. ([^A-Za-z]|$) does the same except it checks for the end of the string
the gi is a flag stating a global search (don't just stop after the first match) and case insensitive
This site I find is invaluable for testing regular expressions
I'm almost there! Just can't figure out the last part of what I need... forming the array.
I want to go through an html file and extract url's from between the phrases playVideo(' and ')
For testing purposes I am just trying to get it to work on the variable str, eventually this will be replaced by the html document:
<script type="text/javascript">
var str ="playVideo('url1') BREAK playVideo('url2') BREAK playVideo('url3')";
var testRE = str.match("playVideo\\(\'(.*?)\'");
alert(testRE[1]);
</script>
This will output 'url1' but if I change it to alert(testRE[2]) it is undefined. How can I get it to fill out the array with all of the URLs (eg testRE[2] output 'url2' and so on) ?
Thanks for any help, new to regex.
Cannot comment, why is that, but adding that by iterating on the regex you get access to the groups;
var str ="playVideo('url1') BREAK playVideo('url2') BREAK playVideo('url3')";
var re = /playVideo\('(.*?)'\)/g;
while (match = re.exec(str)) {
alert(match[1]);
}
Normally a javascript regular expression will just perform the first match. Update your regular expression to add the g modifier. Unfortunately JavaScript regular expressions don't have a non-capturing group so you have to do a little more processing to extract the bit you want e.g.
<script type="text/javascript">
var str ="playVideo('url1') BREAK playVideo('url2') BREAK playVideo('url3')";
var testRE = str.match(/playVideo\(\'[^']*\'/g);
var urls = [];
for (var i = 0; i < testRE.length; i++)
{
urls[i] = testRE[i].substring(11).match(/[^']*/);
}
alert(urls[1]);
alert(urls[2]);
</script>
I'm trying to create a Regex with jQuery so it will search for two words in an attribute of an XML file.
Can someone tell me how to return a result that contains BOTH words (sport and favorite) in any order and any case (upper or lower case)?
var regex = new RegExp("sport favorite", 'i');
var $result = $(data).filter(function() {
if($(this).attr('Description')) {
return $(this).attr('Description').match(regex);
}
});
If they may be separated by any character, you could do it like this:
var regex = new RegExp(".*sport.+favorite.*|.*favorite.+sport.*", 'i');
(This assumes that no other word in the attribute contains the substring favorite or sport.)
var regex = new RegExp("sport favorite|favorite sport", 'i');
The "\b" marker can be used to "match" the edges of words, so
var regex = /\bsport\b.*\bfavorite\b|\bfavorite\b.*\bsport\b/i;
matches the words only as words, and (for example) won't match "sporting his favorite hat".