I have an regular expression built in Java.
String expr = "<a.*?id=\"(pres.*?)\".*?>Discharge.*?Medications:</a>";
I want to use same regular expression in Javascript. Will it be different or same?
I have an regular expression built in Java.
String expr = "<a.*?id=\"(pres.*?)\".*?>Discharge.*?Medications:</a>";
I want to use same regular expression in Javascript. Will it be different or same?
I suggest you don't do it.
Why? I will just point you out to one of the most upvoted answer on StackOverflow: https://stackoverflow.com/a/1732454/851498
In javascript, you'd better use the DOM than a regex to parse HTML.
Example:
var a = document.querySelectorAll('a');
for ( var i = 0, len = a.length; i < len; i++ ) {
// If the id starts with "pres"
if ( a[ i ].id.substr( 0, 3 ) === 'pres' ) {
// check a[ i ].textContent
}
}
Or if you're using jQuery:
$('a').each( function() {
if ( this.id.substr( 0, 3 ) === 'pres' ) {
// check $( this ).text()
}
} );
I didn't code the last part, but you get the idea. Using a regex on $( this ).text()
would be fine, it's not HTML.
Not much different. Regex's in JavaScript are between /
's. So it would look like this:
var expr = /<a.*?id=\"(pres.*?)\".*?>Discharge.*?Medications:</a>/;
Edit, wrong slash. Oops.
Alternatively, you can do:
var expr = new RegExp("<a.*?id=\"(pres.*?)\".*?>Discharge.*?Medications:</a>");
For more details on RegExp in JavaScript see the MDN docs: https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions
It will be the same. All the mainstream regular expression engines support the same basic syntax for regular expressions. Some add their own features on top, but your example isn't doing anything outlandish.