I have string like "abcdefecg asdasda(SIAD) (EA91)" what i need is to get EA91.
I use regular expression to get content in parens but this give me only first existence.
var regExpTextBetweenBraces = /\(([^)]+)\)/;
var res = "abcdefecg asdasda (SIAD) (EA91)".match(regExpTextBetweenBraces);
res[1] will contain SIAD, but i need EA91. How can i do this. Any help ?
Just use the global g flag on your regex:
var regExpTextBetweenBraces = /\(([^)]+)\)/g;
Try /\([^)]+\).*?\(([^)]+)\)/ It'll look for a (something) (Matched) all you needed to do was to tell it to not match the first brace. Or you could use a global match and use an index.
Related
I need a RegEx pattern to match any/all instances that look like {!(.*)}
I have tried the following:
ie.
var mergefield_array = value.match(patt);
where value = '{!lat},{!lng}'
and patt = /{!(.*)}/
it returns a single result: {!lat},{!lng}
but I want it to return two matches in this case ('{!lat}' and '{!lng}').
How do I do it?
Colin G
Your regex is greedy in nature due to use of .* hence matching first { to last }, grabbing anything on the way.
To fix you can make it non-greedy:
patt = /{!(.*?)}/g
Or use negation:
patt = /{!([^}]*)}/g
I have a string that will look something like this:
I'm sorry the code "codehere" is not valid
I need to get the value inside the quotes inside the string. So essentially I need to get the codehere and store it in a variable.
After some researching it looks like I could loop through the string and use .charAt(i) to find the quotes and then pull the string out one character at a time in between the quotes.
However I feel there has to be a simpler solution for this out there. Any input would be appreciated. Thanks!
You could use indexOf and lastIndexOf to get the position of the quotes:
var openQuote = myString.indexOf('"'),
closeQuote = myString.lastIndexOf('"');
Then you can validate they are not the same position, and use substring to retrieve the code:
var code = myString.substring(openQuote, closeQuote + 1);
Regex:
var a = "I'm sorry the code \"codehere\" is not valid";
var m = a.match(/"[^"]*"/ig);
alert(m[0]);
Try this:
var str = "I'm sorry the code \"cod\"eh\"ere\" is not valid";
alert(str.replace(/^[^"]*"(.*)".*$/g, "$1"));
You could use Javascript's match function. It takes as parameter, a regular expression. Eg:
/\".*\"/
Use regular expressions! You can find a match using a simple regular expressions like /"(.+)"/ with the Javascript RegExp() object. Fore more info see w3schools.com.
Try this:
var msg = "I'm sorry the code \"codehere\" is not valid";
var matchedContent = msg.match(/\".*\"/ig);
//matchedContent is an array
alert(matchedContent[0]);
You should use a Regular Expression. This is a text pattern matcher that is built into the javascript language. Regular expressions look like this: /thing to match/flags* for example, /"(.*)"/, which matches everything between a set of quotes.
Beware, regular expressions are limited -- they can't match nested things, so if the value inside quotes contains quotes itself, you'll end up with a big ugly mess.
*: or new RegExp(...), but use the literal syntax; it's better.
You could always use the .split() string function:
var mystring = 'I\'m sorry the code "codehere" is not valid' ;
var tokens = [] ;
var strsplit = mystring.split('\"') ;
for(var i=0;i<strsplit.length;i++) {
if((i % 2)==0) continue; // Ignore strings outside the quotes
tokens.push(strsplit[i]) ; // Store strings inside quotes.
}
// Output:
// tokens[0] = 'codehere' ;
I have some text
I01:00:00:05
I01:00:00:04
I01:00:00:03
I01:00:00:02
I01:00:00:01
Is there a regex that will find each one?
I tried:
var locs = txt.match(/(([A-Z]\d\d\:\d\d\:\d\d\:\d\d)+)+/);
And it finds 5 copies of the first pattern that matches.
Is there a way to get an array with each of them in it?
Thanks
if you include the 'g' flag at the end it should work.
var locs = txt.match(/(([A-Z]\d\d\:\d\d\:\d\d\:\d\d)+)+/g);
You are simply missing the g flag on your regexp and it will find all the occurences that match the pattern.
Like so:
var locs = txt.match(/(([A-Z]\d\d\:\d\d\:\d\d\:\d\d)+)+/g);
of course if that pattern is actually good ;)
Write your regular expression to match a single instance, and use the global specifier /g to get a collection of matches (note the modification to regular expression):
var locs = txt.match(/([A-Z]\d\d\:\d\d\:\d\d\:\d\d)/g);
http://rubular.com/r/L2ZNyz2yJy
Indeed in this case the "+" sign is not necessary:
var locs = txt.match(/[A-Z]\d\d\:\d\d\:\d\d\:\d\d/g);
locs will be ["I01:00:00:05", "I01:00:00:04", "I01:00:00:03", "I01:00:00:02", "I01:00:00:01"]
.
What would be a good way to do this. I have a string with lots of "<" and > and I want to replace them with < and >. So i wrote this:
var str = </text><word34212>
var p = str.replace('\<\;','\<');
var m = p.replace('\>\;','\>');
but that's just doing the first instance of each - and subsequent instances of </> are not replaced. I considered first counting the instances of the < and then looping and replacing one instance of the code on every iteration...and then doing the same for the > but obviously this is quite long-winded.
Can anyone suggest a neater way to do this?
To replace multiple occurances you use a regular expression, so that you can specify the global (g) flag:
var m = str.replace(/</g,'<').replace(/>/g,'>');
Taken from: http://www.bradino.com/javascript/string-replace/
The JavaScript function for String
Replace replaces the first occurrence
in the string. The function is similar
to the php function str_replace and
takes two simple parameters. The first
parameter is the pattern to find and
the second parameter is the string to
replace the pattern with when found.
The javascript function does not
Replace All...
To ReplaceAll you have to do it a
little differently. To replace all
occurrences in the string, use the g
modifier like this:
str = str.replace(/find/g,”replace”)
You need to use the global modifier:
var p = str.replace(/\<\;/g,'\<');
You need to use de /g modifier in your regex and it'll work. Check this page for an example : http://www.w3schools.com/jsref/jsref_replace.asp
I thing a associative array [regex -> replacement] and one iteration would do it
I'm guessing this is a simple problem, but I'm just learning...
I have this:
var location = (jQuery.url.attr("host"))+(jQuery.url.attr("path"));
locationClean = location.replace('/',' ');
locationArray = locationClean.split(" ");
console.log(location);
console.log(locationClean);
console.log(locationArray);
And here is what I am getting in Firebug:
stormink.net/discussed/the-ideas-behind-my-redesign
stormink.net discussed/the-ideas-behind-my-redesign
["stormink.net", "discussed/the-ideas-behind-my-redesign"]
So for some reason, the replace is only happening once? Do I need to use Regex instead with "/g" to make it repeat? And if so, how would I specifiy a '/' in Regex? (I understand very little of how to use Regex).
Thanks all.
Use a pattern instead of a string, which you can use with the "global" modifier
locationClean = location.replace(/\//g,' ');
The replace method only replaces the first occurance when you use a string as the first parameter. You have to use a regular expression to replace all occurances:
locationClean = location.replace(/\//g,' ');
(As the slash characters are used to delimit the regular expression literal, you need to escape the slash inside the excpression with a backslash.)
Still, why are you not just splitting on the '/' character instead?
You could directly split using the / character as the separator:
var loc = location.host + location.pathname, // loc variable used for tesing
locationArray = loc.split("/");
This can be fixed from your javascript.
SYNTAX
stringObject.replace(findstring,newstring)
findstring: Required. Specifies a string value to find. To perform a global search add a 'g' flag to this parameter and to perform a case-insensitive search add an 'i' flag.
newstring: Required. Specifies the string to replace the found value from findstring
Here's what ur code shud look like:
locationClean = location.replace(new RegExp('/','g'),' ');
locationArray = locationClean.split(" ");
njoi'