I have the following string:
[assembly: AssemblyVersion("1.0.0.0")]
The issue now is that I have to extract the 1.0.0.0 out. Here's the regular expression that I can come out with:
var pattern = "[^\\/]+\\[[a-z]+:\\s" + "AssemblyVersion"+ "(?:Attribute)?\\((.+)\\)\\]" ;
var theString ="[assembly: AssemblyVersion("1.0.0.0")]";
var reAssemblyVersion = new RegExp(pattern,"m");
reAssemblyVersion.exec(theString);
var theAnswer = RegExp.$1; // theAnswer is "1.0.0.0", but I want it to be 1.0.0.0
There must be something I did wrong in setting up the pattern variable, but couldn't find out.. any ideas?
Your RegEx did not eliminate the double qoutes.
Here is the right one:
var pattern = "[^\\/]+\\[[a-z]+:\\s" + "AssemblyVersion"+ "(?:Attribute)?\\(\\\"(.+)\\\"\\)\\]" ;
// Here --------------------------------------------------------------------^^^^ ^^^^
Hope it helps
Why can't you just have something simple as this:
\(\"([0-9.]*)\"\)
Related
Suppose I have a sting like this: ABC5DEF/G or it might be ABC5DEF-15 or even just ABC5DEF, it could be shorter AB7F, or AB7FG/H.
I need to create a javascript variable that contains the substring only up to the '/' or the '-'. I would really like to use an array of values to break at. I thought maybe to try something like this.
...
var srcMark = array( '/', '-' );
var whereAt = new RegExp(srcMark.join('|')).test.str;
alert("whereAt= "+whereAt);
...
But this returns an error: ReferenceError: Can't find variable: array
I suspect I'm defining my array incorrectly but trying a number of other things I've been no more successful.
What am I doing wrong?
Arrays aren't defined like that in JavaScript, the easiest way to define it would be with:
var srcMark = ['/','-'];
Additionally, test is a function so it must be called as such:
whereAt = new RegExp(srcMark.join('|')).test(str);
Note that test won't actually tell you where, as your variable suggests, it will return true or false. If you want to find where the character is, use String.prototype.search:
str.search(new RegExp(srcMark.join('|'));
Hope that helps.
You need to use the split method:
var srcMark = Array.join(['-','/'],'|'); // "-|/" or
var regEx = new RegExp(srcMark,'g'); // /-|\//g
var substring = "222-22".split(regEx)[0] // "222"
"ABC5DEF/G".split(regEx)[0] // "ABC5DEF"
From whatever i could understand from your question, using this RegExp /[/-]/ in split() function will work.
EDIT:
For splitting the string at all special characters you can use new RegExp(/[^a-zA-Z0-9]/) in split() function.
var arr = "ABC5DEF/G";
var ans = arr.split(/[/-]/);
console.log(ans[0]);
arr = "ABC5DEF-15";
ans = arr.split(/[/-]/);
console.log(ans[0]);
// For all special characters
arr = "AB7FG/H";
ans = arr.split(new RegExp(/[^a-zA-Z0-9]/));
console.log(ans[0]);
You can use regex with String.split.
It will look something like that:
var result = ['ABC5DEF/G',
'ABC5DEF-15',
'ABC5DEF',
'AB7F',
'AB7FG/H'
].map((item) => item.split(/\W+/));
console.log(result);
That will create an Array with all the parts of the string, so each item[0] will contain the text till the / or - or nothing.
If you want the position of the special character (non-alpha-numeric) you can use a Regular Expression that matches any character that is not a word character from the basic Latin alphabet. Equivalent to [^A-Za-z0-9_], that is: \W
var pattern = /\W/;
var text = 'ABC5DEF/G';
var match = pattern.exec(text);
var position = match.index;
console.log('character: ', match[0]);
console.log('position: ', position);
I'm new to regex. I got a prob here. I work with xpaths strings. I want to remove a particular element from xpath string if the element has id = myVar
Example:
/html/body/div[3]/div[20]/section[1]/p[5]/span[1][#id="var645932"]/span
I want to replace the /span[1][#id="var645932"]/ with just / if my variable value is equal to id value i.e var645932
I need to do it in javascript. all are strings. I prefer regex. if any regex experts are there Kindly help. am stuck here. is it possible to accomplish it without regex ??
Any help are highly appreciated :) TIA
Try this:
var input = '/html/body/div[3]/div[20]/section[1]/p[5]/span[1][#id="var645932"]/span';
var regex = /\w+\[\w+\]\[\#id="var645932"\]\//gi;
input = input.replace(regex, '');
console.log(input);
Example fiddle
This regex is designed to work even if the structure of the HTML changes, eg:
var input = '/html/body/div[3]/div[20]/section[1]/div[6][#id="var645932"]/span';
If you need to set the id in the regex programmatically, use this:
var regex = new RegExp('/\w+\[\w+\]\[\#id="' + id + '"\]\//', 'gi');
You can use below code -
var expr = '/html/body/div[3]/div[20]/section[1]/p[5]/span[1][#id="var645932"]/span';
var idVal = "var645932";
expr = expr.replace('span[1][#id="'+idVal+'"]/','');
DEMO
Well, you could do it in the following way
var id = "var645932";
var regx = new RegExp('/[^/]+?\\[#id="' + id + '"]/');
var str = '/html/body/div[3]/div[20]/section[1]/p[5]/span[1][#id="var645932"]/span';
console.log(str.replace(regx, "/"));
I have a regex and using it in PHP:
(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z])(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))
I want it to use with javascript like:
regexp = new Regexep("here that regular expression");
and check with:
regexp.text(data)
But I couldn't do it work.
Please help me
Thanks,
var matches = data.match(/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z])(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/);
or
var regex = /(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z])(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/;
var matches = regex.test(data);
Like usual fighting with myself to understand regex and a I need a help
here is the string:
str = "function onclick(){location.href='http://localhost.com/default.aspx?sectionid=45674356-346-4447-3456-sddwerwertye&languageid=1';}";
what I need to after regex involved :
Output:
http://localhost.com/default.aspx?sectionid=45674356-346-4447-3456-sddwerwertye&languageid=1
so everything what is between :
"...{location.href=" ---- and --- ";}"
Thanks for any help !!!
var str = "function onclick(){location.href='http://localhost.com/default.aspx?sectionid=45674356-346-4447-3456-sddwerwertye&languageid=1';}";
var m = str.match("location\.href='([^']*)");
var url = m[1];
/location\.href='([^']+)'/
The url is contained within the first group.
str = "function onclick(){location.href='http://localhost.com/default.aspx?sectionid=45674356-346-4447-3456-sddwerwertye&languageid=1';}";
var pattern = /location\.href='([^']+)'/;
if(pattern.test(str)) {
return pattern.exec (str)[1];
}
How about /.*?'([^']*)/?. That's "ignore until the first apostrophe, grab everything that's not an apostrophe".
str = "function onclick(){location.href='http://localhost.com/default.aspx?sectionid=45674356-346-4447-3456-sddwerwertye&languageid=1';}";
str.replace(/.*location.href='(.*)'.*/,"$1");
Would this work in your situation?
In Javascript, how do I determine if some specific HTML is contained within a larger hunk of HTML?
I tried regex:
var htmlstr;
var reg = /<b class="fl_r">Example</b>/.test(htmlstr);
but it doesn't work! Console outputs "missing /". Please, help me fix this.
Regex is a bit of an overkill here. You can just use indexOf like this and not have to worry about escaping things in the string:
var htmlstr1 = 'foo';
var htmlstr2 = 'some stuff <b class="fl_r">Example</b> more stuff';
if (htmlstr1.indexOf('<b class="fl_r">Example</b>') != -1) {
alert("found match in htmlstr1");
}
if (htmlstr2.indexOf('<b class="fl_r">Example</b>') != -1) {
alert("found match in htmlstr2");
}
jsFiddle to play with it is here: http://jsfiddle.net/jfriend00/86Kny/
You need to escape the / character.
Try:
var htmlstr = '<b class="fl_r">Example</b>';
var reg = /<b class="fl_r">Example<\/b>/.test(htmlstr);
Example # http://jsfiddle.net/7cuKe/2/