Javascript Regex: Match Up to Or Between But Not Including - javascript

For instance, I am using a simple regex to match everything before the first space:
var str = '14:00:00 GMT';
str.match(/(.*?)\s/)[0]; //Returns '14:00:00 ' note the space at the end
To avoid this I can do this:
str.match(/(.*?)\s/)[0].replace(' ', '');
But is there a better way? Can I just not include the space in the regex? Another examle is finding something between 2 characters. Say I want to find the 00 in the middle of the above string.
str.match(/:(.*?):/)[0]; //Returns :00: but I want 00
str.match(/:(.*?):/)[0].replace(':', ''); //Fixes it, but again...is there a better way?

I think you just need to change the index from 0 to 1 like this:
str.match(/(.*?)\s/)[1]
0 means the whole matched string, and 1 means the first group, which is exactly what you want.
#codaddict give another solution.
str.match(/(.*?)(?=\s)/)[0]
(?=\s) means lookahead but not consume whitespace, so the whole matched string is '14:00:00' but without whitespace.

You can use positive lookahead assertions as:
(.*?)(?=\s)
which says match everything which is before a whitespace but don't match the whitespace itself.

Yes there is, you can use character classes:
var str = '14:00:00 GMT';
str.match(/[^\s]*/)[0]; //matches everything except whitespace

Your code is quite close to the answer, you just need to replace [0] with [1].
When str.match(/:(.*?):/) is executed, it returns an object, in this example, the object length is 2, it looks like this:
[":00:","00"]
In index 0, it stores the whole string that matches your expression, in index 1 and later, it stores the result that matches the expression in each brackets.
Let's see another example:
var str = ":123abcABC:";
var result = str.match(/:(\d{3})([a-z]{3})([A-Z]{3}):/);
console.log(result[0]);
console.log(result[1]);
console.log(result[2]);
console.log(result[3]);
And the result:
:123abcABC:
123
abc
ABC
Hope it's helpful.

Try this,
.*(?=(\sGMT))
RegexBuddy ScreenShot

Related

Regex to get the text between two characters?

I want to replace a text after a forward slash and before a end parantheses excluding the characters.
My text:
<h3>notThisText/IWantToReplaceThis)<h3>
$('h3').text($('h3').text().replace(regEx, 'textReplaced'));
Wanted result after replace:
notThisText/textReplaced)
I have tried
regex = /([^\/]+$)+/ //replaces the parantheses as well
regex = \/([^\)]+) //replaces the slash as well
but as you can see in my comments neither of these excludes both the slash and the end parantheses. Can someone help?
A pattern like /(?<=\/)[^)]+(?=\))/ won't work in JS as its regex engine does not support a lookbehind construct. So, you should use one of the following solutions:
s.replace(/(\/)[^)]+(\))/, '$1textReplaced$2')
s.replace(/(\/)[^)]+(?=\))/, '$1textReplaced')
s.replace(/(\/)[^)]+/, '$1textReplaced')
s.replace(/\/[^)]+\)/, '/textReplaced)')
The (...) forms a capturing group that can be referenced to with $ + number, a backreference, from the replacement pattern. The first solution is consuming / and ), and puts them into capturing groups. If you need to match consecutive, overlapping matches, use the second solution (s.replace(/(\/)[^)]+(?=\))/, '$1textReplaced')). If the ) is not required at the end, the third solution (replace(/(\/)[^)]+/, '$1textReplaced')) will do. The last solution (s.replace(/\/[^)]+\)/, '/textReplaced)')) will work if the / and ) are static values known beforehand.
You can use str.split('/')
var text = 'notThisText/IWantToReplaceThis';
var splited = text.split('/');
splited[1] = 'yourDesireText';
var output = splited.join('/');
console.log(output);
Try Following: In your case startChar='/', endChar = ')', origString=$('h3').text()
function customReplace(startChar, endChar, origString, replaceWith){
var strArray = origString.split(startChar);
return strArray[0] + startChar + replaceWith + endChar;
}
First of all, you didn't define clearly what is the format of the text which you want to replace and the non-replacement part. For example,
Does notThisText contain any slash /?
Does IWantToReplaceThis contain any parentheses )?
Since there are too many uncertainties, the answer here only shows up the pattern exactly matches your example:
yourText.replace(/(\/).*?(\))/g, '$1textReplaced$2')
var text = "notThisText/IWantToReplaceThis";
text = text.replace(/\/.*/, "/whatever");
output : "notThisText/whatever"`

Add a space to UK Postcode in correct place Javascript

I am trying to write a basic function that will allow me to add a space to UK postcodes where the spaces have been removed.
UK postcodes always have a space before the final digit of the postcode string.
Some examples with no spacing and with correct spacing:
CB30QB => CB3 0QB
N12NL => N1 2NL
OX144FB => OX14 4FB
To find the final digit in the string I am regex /\d(?=\D*$)/g and the Javascript I have in place currently is as follows:
// Set the Postcode
var postCode = "OX144FB";
// Find the index position of the final digit in the string (in this case '4')
var postcodeIndex = postCode.indexOf(postCode.match(/\d(?=\D*$)/g));
// Slice the final postcode at the index point, add a space and join back together.
var finalPostcode = [postCode.slice(0, postcodeIndex), ' ', postCode.slice(postcodeIndex)].join('');
return finalPostcode;
I am getting the following results when I change the set postcost:
CB30QB becomes CB3 0QB - Correct
N12NL becomes N1 2NL - Correct
CB249LQ becomes CB24 9LQ - Correct
OX144FB becomes OX1 44FB - Incorrect
OX145FB becomes OX14 5FB - Correct
It seems that the issue might be to do with having two digits of the same value as most other combinations seem to work.
Does anyone know how I can fix this?
I should use string.replace
string.replace(/^(.*)(\d)/, "$1 $2");
DEMO
You can use replace() with regex, you need to place space before 3 letters from the end
document.write('CB30QB'.replace(/^(.*)(.{3})$/,'$1 $2')+'<br>');
document.write('N12NL'.replace(/^(.*)(.{3})$/,'$1 $2')+'<br>');
document.write('CB249LQ'.replace(/^(.*)(.{3})$/,'$1 $2')+'<br>');
document.write('OX144FB'.replace(/^(.*)(.{3})$/,'$1 $2'));
As everyone else is answering, .replace() is easier. However, let me point what's wrong in the code.
The problem is you're using postCode.indexOf() to find the first occurence of what has been matched. In this case:
Text: OX144FB
Match: ^ match is correct: "4"
Text: OX144FB
IndexOf: ^ first occurence of "4"
To fix it, use the .index of the match object:
// Find the index position of the final digit in the string (in this case '4')
var postcodeIndex = postCode.match(/\d(?=\D*$)/g).index;
var postCode = "OX144FB";
return postCode.replace(/^(.*)(\d)(.*)/, "$1 $2$3");
Using the String.prototype.replace method is obviously the easiest way:
return postCode.replace(/(?=\d\D*$)/, ' ');
or using the greediness:
return postCode.replace(/^(.*)(?=\d)/, '$1 ');
Your previous code doesn't work because you are searching with indexOf the substring matched with the String.prototype.match() method (that is the last digit before the end). But if this digit is several times in the string, indexOf will return the position of the first occurrence.
As an aside, when you want to find the position of a match in a string, use the String.prototype.search() method that returns this position.
This is an old problem, but whilst Avinash Raj's solution works, it only works if all your postcodes are without spaces. If you have a mix, and you want to regularize them to having a single space, you can use this regex:
string.replace(/(\S*)\s*(\d)/, "$1 $2");
DEMO - it even works with more than one space!

RegEx - Get All Characters After Last Slash in URL

I'm working with a Google API that returns IDs in the below format, which I've saved as a string. How can I write a Regular Expression in javascript to trim the string to only the characters after the last slash in the URL.
var id = 'http://www.google.com/m8/feeds/contacts/myemail%40gmail.com/base/nabb80191e23b7d9'
Don't write a regex! This is trivial to do with string functions instead:
var final = id.substr(id.lastIndexOf('/') + 1);
It's even easier if you know that the final part will always be 16 characters:
var final = id.substr(-16);
A slightly different regex approach:
var afterSlashChars = id.match(/\/([^\/]+)\/?$/)[1];
Breaking down this regex:
\/ match a slash
( start of a captured group within the match
[^\/] match a non-slash character
+ match one of more of the non-slash characters
) end of the captured group
\/? allow one optional / at the end of the string
$ match to the end of the string
The [1] then retrieves the first captured group within the match
Working snippet:
var id = 'http://www.google.com/m8/feeds/contacts/myemail%40gmail.com/base/nabb80191e23b7d9';
var afterSlashChars = id.match(/\/([^\/]+)\/?$/)[1];
// display result
document.write(afterSlashChars);
Just in case someone else comes across this thread and is looking for a simple JS solution:
id.split('/').pop(-1)
this is easy to understand (?!.*/).+
let me explain:
first, lets match everything that has a slash at the end, ok?
that's the part we don't want
.*/ matches everything until the last slash
then, we make a "Negative lookahead" (?!) to say "I don't want this, discard it"
(?!.*) this is "Negative lookahead"
Now we can happily take whatever is next to what we don't want with this
.+
YOU MAY NEED TO ESCAPE THE / SO IT BECOMES:
(?!.*\/).+
this regexp: [^\/]+$ - works like a champ:
var id = ".../base/nabb80191e23b7d9"
result = id.match(/[^\/]+$/)[0];
// results -> "nabb80191e23b7d9"
This should work:
last = id.match(/\/([^/]*)$/)[1];
//=> nabb80191e23b7d9
Don't know JS, using others examples (and a guess) -
id = id.match(/[^\/]*$/); // [0] optional ?
Why not use replace?
"http://google.com/aaa".replace(/(.*\/)*/,"")
yields "aaa"

Extracting numbers from a string using regular expressions

I am clueless about regular expressions, but I know that they're the right tool for what I'm trying to do here: I'm trying to extract a numerical value from a string like this one:
approval=not requested^assignment_group=12345678901234567890123456789012^category=Test^contact_type=phone^
Ideally, I'd extract the following from it: 12345678901234567890123456789012 None of the regexes I've tried have worked. How can I get the value I want from this string?
This will get all the numbers:
var myValue = /\d+/.exec(myString)
mystr.match(/assignment_group=([^\^]+)/)[1]; //=> "12345678901234567890123456789012"
This will find everything from the end of "assignment_group=" up to the next caret ^ symbol.
Try something like this:
/\^assignment_group=(\d*)\^/
This will get the number for assignment_group.
var str = 'approval=not requested^assignment_group=12345678901234567890123456789012^category=Test^contact_type=phone^',
regex = /\^assignment_group=(\d*)\^/,
matches = str.match(regex),
id = matches !== null ? matches[1] : '';
console.log(id);
If there is no chance of there being numbers anywhere but when you need them, you could just do:
\d+
the \d matches digits, and the + says "match any number of whatever this follows"

How to remove a number inside brackets at the end of string with regex

Looking to have a recursive function that takes a string and removes the ending '[x]'. For example 'abc [1] [3]' needs to be 'abc [1]'. The string could also be 'abc [1] [5] [2]' and would need to be 'abc [1] [5]'.
I'm trying str.replace(/[\\\[\d\\\]]$/, '') but it only replaces the very last closing bracket and ignores everything else.
Any ideas?
You don't need the outer enclosing brackets. Try: str.replace(/\[\d\]$/, '');
If it is guaranteed that the string always contains a [number], you could just use substring and lastIndexOf:
str = str.substring(0, str.lastIndexOf('['));
Update: Or just add a test:
var index = str.lastIndexOf('[');
if(index > -1) {
str = str.substring(0,index);
}
\[\d+\]$
that should say any bracket followed by any number of digits followed by a bracket, all at the end of the string.
I say "should" because I'm still not as proficient at regex as I'd like to be, but in regexr (a nifty little AIR app for testing regular expressions), it seems to work.
EDIT:
Just in case anybody wants to play around with regexr, it's at http://gskinner.com/RegExr/desktop/. I have no affiliation with it, I just think it's a nice tool to have.
\[\d+\]([^]]*)$ works in Python and should work in Javascript. This allows for trailing bits after the [x], which are left behind. I believe that's why you weren't seeing the expected results, because you left trailing whitespace behind. Also note that I changed the regex to allow x to be any number of digits -- if that's not what you want, remove the +.
Here's the code:
import re
s = 'abc [1] [5] [2]'
while True:
new_s = re.sub(r'\[\d+\]([^]]*)$', r'\1', s)
if new_s == s:
break
s = new_s
print s
and the output:
abc [1] [5]
abc [1]
abc
/(.*)([\[].*[\]]\Z)/ should do it, you will need to do it using a match method, and it will provide two groups in an array, one with your required string, and the other with the ending in it.

Categories