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);
Related
For a given URL,
/disconnect/<backend>/foo/<association_id>/
I'd like to get
/disconnect/:backend/foo/:association_id/
There could be any number of <pattern>s in a path.
Below is a regex to use with replace method
var str = '/disconnect/<backend>/foo/<association_id>/',
reg = /<([^>]+)>/g;
console.log(str.replace(reg, ":$1"));
DEMO
What about this way? Live Demo http://jsfiddle.net/d4N9s/
var mystring = "/disconnect/<backend>/foo/<association_id>/"
var middle = mystring.replace(/>/g , "")
console.log(middle.replace(/</g , ":"));
Cleaner way:
var mapO = {
'>':"",
'<':":",
};
str = mystring.replace(/<|>/gi, function(matched){
return mapO[matched];
});
console.log(str);
/<(.*?)>/g
That will match all instances of a string between < and >. You can use some simple JavaScript to replace each instance pretty easily.
http://regexr.com/3ggen
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 am trying to split a UK postcode string to only include the initial letters. For example, 'AA1 2BB' would become 'AA.'
I was thinking something like the below.
var postcode = 'AA1 2BB';
var postcodePrefix = postcode.split([0-9])[0];
This does not actually work, but can someone help me out with the syntax?
Thanks for any help.
You can try something like this:
var postcode = 'AA1 2BB';
var postcodePrefix =postcode.split(/[0-9]/)[0];
Alternatively, you could use a regex to simply find all alphabetic characters that occur at the beginning of the string:
var postcode = 'AA1 2BB';
var postcodePrefix = postcode.match(/^[a-zA-Z]+/);
If you want any initial characters that are non numeric, you could use:
var postcodePrefix = postcode.match(/^[^0-9]+/);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
"AA1 2BB".split(/[0-9]/)[0];
or
"AA1 2BB".split(/\d/)[0];
var m = postcode.match(/([^\d]*)/);
if (m) {
var prefix = m[0];
}
I'm trying to match javascript files inside /static/js that include ?v=xxxx at the end, 'x' being a character or a number, so it has to match:
http://127.0.0.1:8888/static/js/components/backbone.js?v=a6tsb
But not:
http://127.0.0.1:8888/static/js/views/ribbon.js
http://127.0.0.1:8888/templates/require-config.js
This one matches the hash:
var hashRegex = new RegExp("^.*\\?v=\\w{5}$");
But I'm trying to extend that one to include "/static/js".
I tried:
var hashRegex = new RegExp("^.*\/static\/js\/.*\\?v=\\w{5}$");
But doesn't seems to work.
What am I missing?
In javascript when regex is represented using string you need to double escape(\\) the special character(of regex)
So,your regex would be
var hashRegex = new RegExp("^.*/static/js/.*\\?v=\\w{5}$");
But if you use this syntax for regex
var hashRegex = /regex/;
you have to escape with single \.You would also escape / since it is used as a delimiter
So,your regex in this case would be
var hashRegex = /^.*\/static\/js\/.*\?v=\w{5}$/;
I would try this:
var hashRegex = new RegExp("^.*\/static\/js\/.*\?v\=[a-zA-Z0-9]{5}$");
( I don't know if you have to escape the = )
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.]*)\"\)