I want to extract all digits after last occurrence of character "-" so for example 311-1974-8 should return me 8 and 311-1974-348 should return 348
edit:
Added clarification from a comment:
actually it's an external tool which provides it's own inbuild functionalists and i have no other option but to use regex to extract this. No JS can be applied :(
This captures the last number.
var str = '311-1974-348';
var matches = str.match(/-(\d+)$/);
var match = matches ? matches[1] : null;
console.log("matched? " + match);
Try matching on /[^-]+$/, e.g.:
var s = '311-1974-348';
s.match(/[^-]+$/); // => ["348"]
You mentioned in a comment it's for an external tool so...
-([0-9]+)$
dunno how your tool handles captured groups or anything...
Why not simply spliting ?
var str = input.split('-').pop();
Try this /[0-9]+$/. It worked on both the inputs you provided.
Related
I'm new to regex and trying to figure out how to remove characters till the last - in the string. I currently have strings in the format like this:
purple-hoodie.jpg-1625739747918
I am trying to remove characters to essentially be left with:
-1625739747918
Does anyone have any advice on how to approach this? I'm struggling to work out how to indicate to reach the last - in the string, if that is even possible?
Thanks
Just use lastIndexOf
let str = 'purple-hoodie.jpg-1625739747918'
console.log(str.substring(str.lastIndexOf('-')))
I prefer a match approach here:
var input = "purple-hoodie.jpg-1625739747918";
var output = input.match(/-\d+$/)[0];
console.log("match is: " + output);
But this assumes that the input would end in all digits. A more general regex approach might use a replace all:
var input = "purple-hoodie.jpg-1625739747918";
var output = input.replace(/^.*(?=-)/, "");
console.log("match is: " + output);
Here is my solution.
const txt = 'purple-hoodie.jpg-1625739747918';
const result = txt.replace(/-\d+$/, '');
console.log(result)
This removes the last trailing digits prefixed by -.
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"`
I am trying to fix this since yesterday and believe I am missing something very simple.
I wrote a regex to match ANY ONE of the three IP address format:
Pattern to match : X.X.X.X OR X.X.X.X/X.X.X.X OR X.X.X.X-X.X.X.X
Regex:
/^(([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\/([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5]))|(([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\-([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5]))|(([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5]))$/
Problem:
The regex matches the above 3 formats but the problem is with the alternation symbol - the behaviour is like the regex stops once the match is found.
Example: 1.1.1.1/1.1.1.1 - Once this match is found it does not check after that.
i.e: 1.1.1.1/1.1.1.1 - Valid
But 1.1.1.1/1.1.1.1(...anything after this is also recognized as valid which should not be the case...)
Question:
How do I make it to match only one of the 3 alternatives as it is. I tried a bit with word boundaries (\b) as well, but I am not sure if that is what is needed.
Any help appreciated.
Try this regex:
(^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$)|(^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[\/-](?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$)
You can test it using the following link for verification.
https://regex101.com/r/fC6uS3/1
The problem is that alternation has the lowest precedence of all the regex constructs. Your regex matches either:
^X.X.X.X/X.X.X.X // anchored at start only
or
X.X.X.X-X.X.X.X // not anchored
or
X.X.X.X$ // anchored at end only
You can fix it by adding another set of parentheses around everything but the anchors:
^(your regex)$
Im not sure if you want to match more than one, but if that is the case then remove "^" from the beginning and "$" from the end and also make it globally search like this:
/(([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\/([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5]))|(([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\-([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5]))|(([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5]))/g
Hope this helps
You can probably shorten your regex to:
^(((([0-2][0-5][0-6])|(\d{1,2}))\.){3}((([0-2][0-5][0-6])|(\d{1,2}))))([/-](((([0-2][0-5][0-6])|(\d{1,2}))\.){3}(([0-2][0-5][0-6])|(\d{1,2}))))*$
If you are running it with javascript you would surround with /.../
var pattern = new RegExp(/^(((([0-2][0-5][0-6])|(\d{1,2}))\.){3}((([0-2][0-5][0-6])|(\d{1,2}))))([/-](((([0-2][0-5][0-6])|(\d{1,2}))\.){3}(([0-2][0-5][0-6])|(\d{1,2}))))*$/);
var testCases = {};
//should work
testCases['testCaseA'] = '1.2.3.4';
testCases['testCaseB'] = '1.2.3.4/1.256.3.4';
testCases['testCaseC'] = '1.2.3.4-1.2.3.4';
//should not work
testCases['testCaseD'] = '1.257.3.4';
testCases['testCaseE'] = '1.2.3.4/1.2.3.356';
testCases['testCaseF'] = '1.2.3.4-1.2.3.4I';
var results = '<table><tr><th>Cases</th><th>Inputs</th><th>Outputs</th></tr>';
$.each(testCases, function(k, v) {
results += '<tr><td>' + k + ' </td><td>' + v + ' </td><td>' + pattern.test(v) + '</td>';
});
document.write(results + '</table>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
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"
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, '');