Search through string with Javascript - javascript

Say I have a string like this:
jJKld-xxx-JKl122
Using javascript, how can I get on what's in-between the - characters? In others words, all I need to do is put whatever is xxx into a variable.
Thanks

If the string is always in that format, this will work:
var foo = 'jJKld-xxx-JKl122';
var bar = foo.split('-')[1]; // = xxx

just try it with this simple regex
var str = 'jJKld-xxx-JKl122';
var xxx = str.replace( /^[^\-]*-|-[^\-]*$/g, '' );

You can simply use the following regex to get the result
var myString = "jJKld-xxx-JKl122";
var myRegexp = /(?:^|\s*)-(.*?)-(?:^|\s*)/g;
var match = myRegexp.exec(myString);
alert(match[1]);
See the demo here

Related

Javascript Regex: replacing second set of square brackets

I have a string that looks something like this:
"something[something][0][]"
What I need to do is replace the [0] with another number. This is what I've attempted:
var name = nameVar.replace(/[^\[\]]+(?=\]\[[^\]]+\]$)/, "999");
Any help on the regex expression ?
You can use a capturing group for first [...]:
var re = /(\[[^\]]*\])\[[^\]]*\]/;
var str = 'something[something][0][]';
var result = str.replace(re, '$1[999]');
RegEx Demo
Please try:
function myFunction() {
var nameVar = "something[something][0][]";
var name = nameVar.replace(/([^[]+\[[^\]]+\])(\[[^\]]+\])/, '$1[999]');
alert(name);
}
myFunction()

Pattern match to get string between brackets

I am trying to find whatever string is in between the aggregate() and find(). Below is my code.
var str1 = 'aggregate([{$group:{_id:{state:"$state",city:"$city"},sum:{$sum:"$pop"}}},{$sort:{sum:1}},{$group:{_id:"$_id.state",smallestcity:{$first:"$_id.city"},smallest:{$first:"$sum"},largestcity:{$last:"$_id.city"},largest:{$last:"$sum"}}}])'
var str2 = 'find({awards:{$elemMatch:{award:"Turing Award",year:{$gt:1980}}}}).limit(0)'
var matchPharse = /((.*))/;
var result = str1.match(matchPharse);
console.log(result);
I am getting the result always the whole string instead of
[{$group:{_id:{state:"$state",city:"$city"},sum:{$sum:"$pop"}}},{$sort:{sum:1}},{$group:{_id:"$_id.state",smallestcity:{$first:"$_id.city"},smallest:{$first:"$sum"},largestcity:{$last:"$_id.city"},largest:{$last:"$sum"}}}]
I am searching for something like this
try this pattern instead:
var matchPharse = /((\[.*\]))/;
((\[.*?\]))
You Should use a non greedy expression.
Try the following RegEx:
var matchPharse= /\((.*)\)/g;
Matches any sequence between ().
This is a DEMO.
var str1 = 'aggregate([{$group:{_id:{state:"$state",city:"$city"},sum:{$sum:"$pop"}}},{$sort:{sum:1}},{$group:{_id:"$_id.state",smallestcity:{$first:"$_id.city"},smallest:{$first:"$sum"},largestcity:{$last:"$_id.city"},largest:{$last:"$sum"}}}])'
var str2 = 'find({awards:{$elemMatch:{award:"Turing Award",year:{$gt:1980}}}}).limit(0)'
var matchPharse = /\((.*)\)/;
var result = str1.match(matchPharse);
alert(result);
You just need to escape the outside parentheses. Try:
var matchPharse = /\((.*)\)/;
For just the content inside the parentheses use result[1]

Javascript regex value search

After reading the post here, I tried to get the value by regex as below:
var myString = "<a href='/search.html?id=HDJ&area=ASD&estate=JKG&ppt=3'></a>";
var myRegexp = /&estate=(.*?)(?:\s|$)/g;
var match = myRegexp.exec(myString);
match[1]
The result was JKG&propertytype=3'></a>, but I only want JKG. Strictly speaking I want the value between &estate= and &ppt Could someone suggest how to do that?
Thanks
A Regular Expression:
/&estate=(.*?)&ppt=/g
Note: I wouldn't recommend using regular expressions to parse query strings. It's brittle. Consider if the variables in the query string change order. If that can be the case, I recommend reading - Parse query string in JavaScript.
do:
var myString = "<a href='/search.html?id=HDJ&area=ASD&estate=JKG&ppt=3'></a>";
var myRegexp = /estate=(.*)&ppt=/g;
var match = myRegexp.exec(myString);
console.log( match[1] );
If only the solution is important then you can use the following:
var myString = "<a href='/search.html?id=HDJ&area=ASD&estate=JKG&ppt=3'></a>";
var indx1 = mystring.indexOf("&estate=") + 8;
var indx2 = mystring.indexOf("&ppt");
var neededString = mystring.substring(indx1, indx2);
Just exclude ampersands from the selection:
var myRegexp = /&estate=([^&]+)/g;
You might want to change it to this, in case estate is the first parameter:
var myRegexp = /[\?&]estate=([^&]+)/g;
jsFiddle
Based on your result value, just split it at the ampersand, no new or alternate regex required.
JavaScript:
var myString = "<a href='/search.html?id=HDJ&area=ASD&estate=JKG&ppt=3'></a>";
var myRegexp = /&estate=(.*?)(?:\s|$)/g;
var match = myRegexp.exec(myString);
var value = match[1].split('&')[0];
alert( value );

get particular string part in javascript

I have a javascript string like "firstHalf_0_0_0" or secondHalf_0_0_0". Now I want to get the string before the string "Half" from above both strings using javascript.Please help me.
Thanks.
var myString = "firstHalf_0_0_0";
var parts = myString.split("Half");
var thePart = parts[0];
var str = 'firstHalf_0_0_0',
part = str.match(/(\w+)Half/)[1];
alert(part); // Alerts "first"
var str = "firstHalf.....";
var index = str.indexOf("Half");
var substring = str.substr(0, index);
jsFiddle demo.
Using this you can get any particular part of string.
var str= 'your string';
var result = str.split('_')[0];
Working example here for your particular case.
http://jsfiddle.net/7kypu/3/
cheers!

Regular expression to get numbers from the string in javascript

I have a String like the following
minmaxSize2-8
minmaxSize12-20
How to get the range from the above strings.I need to get 2-8 and 12-20.
Please suggest the regular expressions in javascript
You can do it like this:
var myString = "minmaxSize12-20";
var myRegexp = /(\d+)-(\d+)/g; // Numbers dash Numbers
var match = myRegexp.exec(myString);
alert(match[1]); // 12
alert(match[2]); // 20
Something like this should work:
var str = 'minmaxSize12-20';
var range = str.replace(/^.*?Size/i, ''); // returns 12-20
Simply :
"minmaxSize12-20".match(/(\d+)-(\d+)/)
or even
/(\d+)-(\d+)/.exec("minmaxSize12-20");

Categories

Resources