I need to capture this: <!.This is a string.!>
It can contain anything within the <!. .!>
So I tried this: <\!\..+\.\!>/g
Which didn't work. I've been trying to fix this for days now with no success. I want to capture the whole string including the <!. .!>
Thanks for the help!
EDIT
Since the OP edited his post to specify that he doesn't just want to capture the string but also the delimiters, I have moved out the capturing parentheses from <!\.(.*?)\.!> to (<!\..*?\.!>)
This should do it:
var myregexp = /(<!\..*?\.!>)/m;
var match = myregexp.exec(subject);
if (match != null) {
result = match[1];
} else {
result = "";
}
Since JS does not support lookbehinds, the idea is to match the whole string, including the delimiters, but to only capture "My String" into Group 1. Then we inspect Group 1.
For someone that's "pretty good at Regex", this one is a no brainier.
var subject = "<!.This is a string.!>";
subject = subject.match(/<!\.(.*?)\.!>/);
console.log(subject[1]);
http://jsfiddle.net/tuga/YzLnN/1/
Related
I have a url like http://www.somedotcom.com/all/~childrens-day/pr?sid=all.
I want to extract childrens-day. How to get that? Right now I am doing it like this
url = "http://www.somedotcom.com/all/~childrens-day/pr?sid=all"
url.match('~.+\/');
But what I am getting is ["~childrens-day/"].
Is there a (definitely there would be) short and sweet way to get the above text without ["~ and /"] i.e just childrens-day.
Thanks
You could use a negated character class and a capture group ( ) and refer to capture group #1. The caret (^) inside of a character class [ ] is considered the negation operator.
var url = "http://www.somedotcom.com/all/~childrens-day/pr?sid=all";
var result = url.match(/~([^~]+)\//);
console.log(result[1]); // "childrens-day"
See Working demo
Note: If you have many url's inside of a string you may want to add the ? quantifier for a non greedy match.
var result = url.match(/~([^~]+?)\//);
Like so:
var url = "http://www.somedotcom.com/all/~childrens-day/pr?sid=all"
var matches = url.match(/~(.+?)\//);
console.log(matches[1]);
Working example: http://regex101.com/r/xU4nZ6
Note that your regular expression wasn't actually properly delimited either, not sure how you got the result you did.
Use non-capturing groups with a captured group then access the [1] element of the matches array:
(?:~)(.+)(?:/)
Keep in mind that you will need to escape your / if using it also as your RegEx delimiter.
Yes, it is.
url = "http://www.somedotcom.com/all/~childrens-day/pr?sid=all";
url.match('~(.+)\/')[1];
Just wrap what you need into parenteses group. No more modifications into your code is needed.
References: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
You could just do a string replace.
url.replace('~', '');
url.replace('/', '');
http://www.w3schools.com/jsref/jsref_replace.asp
I am writing a little app for Sharepoint. I am trying to extract some text from the middle of a field that is returned:
var ows_MetaInfo="1;#Subject:SW|NameOfADocument
vti_parservers:SR|23.0.0.6421
ContentTypeID:SW|0x0101001DB26Cf25E4F31488B7333256A77D2CA
vti_cachedtitle:SR|NameOfADocument
vti_title:SR|ATitleOfADocument
_Author:SW:|TheNameOfOurCompany
_Category:SW|
ContentType:SW|Document
vti_author::SR|mrwienerdog
_Comments:SW|This is very much the string I need extracted
vti_categories:VW|
vtiapprovallevel:SR|
vti_modifiedby:SR|mrwienerdog
vti_assignedto:SR|
Keywords:SW|Project Name
ContentType _Comments"
So......All I want returned is "This is very much the string I need extracted"
Do I need a regex and a string replace? How would you write the regex?
Yes, you can use a regular expression for this (this is the sort of thing they are good for). Assuming you always want the string after the pipe (|) on the line starting with "_Comments:SW|", here's how you can extract it:
var matchresult = ows_MetaInfo.match(/^_Comments:SW\|(.*)$/m);
var comment = (matchresult==null) ? "" : matchresult[1];
Note that the .match() method of the String object returns an array. The first (index 0) element will be the entire match (here, we the entire match is the whole line, as we anchored it with ^ and $; note that adding the "m" after the regex makes this a multiline regex, allowing us to match the start and end of any line within the multi-line input), and the rest of the array are the submatches that we capture using parenthesis. Above we've captured the part of the line that you want, so that will present in the second item in the array (index 1).
If there is no match ("_Comments:SW|" doesnt appear in ows_MetaInfo), then .match() will return null, which is why we test it before pulling out the comment.
If you need to adjust the regex for other scenarios, have a look at the Regex docs on Mozilla Dev Network: https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions
You can use this code:
var match = ows_MetaInfo.match(/_Comments:SW\|([^\n]+)/);
if (match)
document.writeln(match[1]);
I'm far from competent with RegEx, so here is my RegEx-less solution. See comments for further detail.
var extractedText = ExtractText(ows_MetaInfo);
function ExtractText(arg) {
// Use the pipe delimiter to turn the string into an array
var aryValues = ows_MetaInfo.split("|");
// Find the portion of the array that contains "vti_categories:VW"
for (var i = 0; i < aryValues.length; i++) {
if (aryValues[i].search("vti_categories:VW") != -1)
return aryValues[i].replace("vti_categories:VW", "");
}
return null;
}
Here's a working fiddle to demonstrate.
I am trying to target ?state=wildcard in this statement :
?state=uncompleted&dancing=yes
I would like to target the entire line ?state=uncomplete, but also allow it to find whatever word would be after the = operator. So uncomplete could also be completed, unscheduled, or what have you.
A caveat I am having is granted I could target the wildcard before the ampersand, but what if there is no ampersand and the param state is by itself?
Try this regular expression:
var regex = /\?state=([^&]+)/;
var match = '?state=uncompleted&dancing=yes'.match(regex);
match; // => ["?state=uncompleted", "uncompleted"]
It will match every character after the string "\?state=" except an ampersand, all the way to the end of the string, if necessary.
Alternative regex: /\?state=(.+?)(?:&|$)/
It will match everything up to the first & char or the end of the string
IMHO, you don't need regex here. As we all know, regexes tend to be slow, especially when using look aheads. Why not do something like this:
var URI = '?state=done&user=ME'.split('&');
var passedVals = [];
This gives us ['?state=done','user=ME'], now just do a for loop:
for (var i=0;i<URI.length;i++)
{
passedVals.push(URI[i].split('=')[1]);
}
Passed Vals wil contain whatever you need. The added benefit of this is that you can parse a request into an Object:
var URI = 'state=done&user=ME'.split('&');
var urlObjects ={};
for (var i=0;i<URI.length;i++)
{
urlObjects[URI[i].split('=')[0]] = URI[i].split('=')[1];
}
I left out the '?' at the start of the string, because a simple .replace('?','') can fix that easily...
You can match as many characters that are not a &. If there aren't any &s at all, that will of course also work:
/(\?state=[^&]+)/.exec("?state=uncompleted");
/(\?state=[^&]+)/.exec("?state=uncompleted&a=1");
// both: ["?state=uncompleted", "?state=uncompleted"]
I am facing this problem. i am getting strings like this.
'=--satya','=---satya1','=-----satya2'.
now my problem is i have to remove these special characters and print the strings like this
'satya'
'satya1'
'satya2'
please help to solve this problem?
Use String.replace:
var s = '=---satya1';
s.replace(/[^a-zA-Z0-9]/g, '');
to replace all non-letter and non-number characters or
s.replace(/[-=]/g, '');
to remove all - and = characters or even
'=---satya-1=test'.replace(/(=\-+)/g, ''); // out: "satya-1=test"
to prevent removing further - or =.
You could extract that information with a regular expression such as
/\'\=-{0,}(satya[0-9]{0,})\'/
Live example: http://jsfiddle.net/LFZje/
The regex matches
Literal '
Literal =
Zero or more -
Starts a capture group and captures
- Literal satya
- Zero or more numbers
Ends the capture group
Literal '
Then using code such as
var regex = /\'\=-{0,}(satya[0-9]{0,})\'/g;
while( (match = regex.exec("'=--satya','=---satya1','=-----satya2'")) !== null)
{
// here match[0] is the entire capture
// and match[1] is tthe content of the capture group, ie "satya1" or "satya2"
}
See the live example more detail.
Use javascript function replace which helps you to use regex for this case
var string = '=---satya1';
string = string.replace(/[^a-zA-Z0-9]/g, '');
I am building a 'keyword' highlighting script and I need to write a regular expression to parse the following url, to find the searched for keywords to highlight.
I am REALLY bad with regex, so I was hoping someone who rocks it, could help.
Our search string uses "skw" as the parameter and "%2c" (comma) to separate terms, with "+" for spaces.
Example URLS:
http://[url].com/Search.aspx?skw=term1
http://[url].com/Search.aspx?skw=term1%2c+term2
Is there a single RegExp that I can use that will give me a collection that looks like this?
var "http://[url].com/Search.aspx?skw=term+one%2c+term2".match([Expression]);
matches[0] = "term one"
matches[1] = "term2"
Thanks! Any help is greatly appreciated.
You can't do this with a single match, but you can do it with a matches, a replace and a split:
url.match(/\?(?:.*&)?skw=([^&]*)/)[1].replace(/\+/g, " ").split('%2c')
You may want to do the match separately and bail out if the match fails (which it could if yur URL didn't have an skw parameter).
You probably really want to do an unescape too, to handle other escaped characters in the query:
unescape(url.match(/\?(?:.*&)?skw=([^&]*)/)[1].replace(/\+/g, " ")).split(',')
https?://[^\?]+\?skw=([^%]*)(?:%2c\+*(.*))?
In javascript this is
var myregexp = /https?:\/\/[^\?]+(?:\?|\?[^&]*&)skw=([^%]*)(?:%2c\+*(.*))?/;
var match = myregexp.exec(subject);
if (match != null && match.length > 1) {
var term1 = match[1];
var term2 = match[2];
}
EDIT:
Sorry, I re-read your question, to handle multiple terms you need to combine this with a split
var subject = "http://[url].com/Search.aspx?skw=term1+one%2c+term2";
var myregexp = /https?:\/\/[^\?]+(?:\?|\?[^&]*&)skw=([^&]*)?/;
var match = myregexp.exec(subject);
if (match != null && match.length > 1) {
var terms = unescape(match[1]).split(",");
}
This task doesn't really lend itself to a single regular expression. Check out Parsing Query Strings in JavaScript for a script to assist you.