JavaScript Regex Expression needed for split method [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to remove characters after numbers and needs to include .pdf extension.
I cannot use index since the file name varies on dates and names.
ex: My File Name : Mar 1316 and A - sec.pdf
required output file name: Mar1316.pdf
can anyone help with the regex and split method?

If there can be only one sequence of numbers or you want until the last sequence you can use this regex: (.*[1-9]+)
.*: any character any number of time
[1-9]+: 1 number or more
const str = "Mar 1316 and A - sec.pdf";
const match = /.*[1-9]+/.exec(str);
if (match) {
// will equal 'Mar 1316.pdf'
const newStr = str.slice(0, match[0].length) + '.pdf';
}

I don't think you can do this with JS regex, simply because you need both lookbehind and lookahead in the same regex, which I believe you cannot do.
/\s+|(?<=\d).*(?=\.)/

Related

Remove everything from string after the second - with a single line in Javascript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to ensure the language is always extracted from a string, only before the 2nd dash (-)
So
en-AU-Option-A
becomes
en-AU
Is this possible with a single line of Javascript?
Split takes a second argument to specify no. of element desired. so in this case you can specify 2 and it will split upto 2nd - and than join -
let str = `en-AU-Option-A`
console.log(str.split('-',2).join('-'))
Of course it is possible. This is not using regex.
let mystr = 'en-AU-Option-A'
console.log(mystr.split('-').slice(0, 2).join('-'))
Here is a regex solution
var regex = /^([a-z]+|[A-Z]+)\-([a-z]+|[A-Z]+)/g;
var str = 'en-AU-Option-A';
console.log(str.match(regex)) // ["en-AU"]
Try This:
var patt = /[^-]*-[^-]*/ig ;
console.log( patt.exec( 'en-AU-Option-A' )[0] )

Remove leading + or 00 from number using regex [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need to remove + or 00 from the beginning of a number in case they exist. So a number like +37253783478 would output 37253783478 and 0037253783478 would output 37253783478. What would the regex look like that matches this pattern?
EDIT: I've managed to remove the leading zeros using ^0+ but I can't figure out how to match both cases.
If I understand the requirement, the following will match both cases. Essentially, what you need to do is use the regex or operator |.
The following will remove all leading 0s
str.replace(/(^0+|^\+)/,'')
But if you just need to remove exactly two leading 0s, use this:
str.replace(/(^00|^\+)/,'')
And here it is in action on your examples:
let nums = ['+37253783478', '0037253783478', '0037253780478', '375378+0478'];
let replaced = nums.map(num => num.replace(/(^0+|^\+)/,''));
console.log(replaced);

Full-width Numbers convert to half-width Numbers in jQuery / JS [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
A would like to convert Full-width Numbers (e.g. 123) to half-width Numbers (e.g. 123). I found code do this in PHP but not in JS. Could anyone helps? Thanks.
function fullWidthNumConvert(fullWidthNum){
// Magic here....
....
return halfWidthNum;
}
Do a string .replace(), using a regular expression to match the characters in question. The callback in .replace()'s second argument can get the character code of the matched character and subtract from that to get the character code of the standard digit, then convert that back to a string.
function fullWidthNumConvert(fullWidthNum){
return fullWidthNum.replace(/[\uFF10-\uFF19]/g, function(m) {
return String.fromCharCode(m.charCodeAt(0) - 0xfee0);
});
}
console.log(fullWidthNumConvert("0123456789"));
console.log(fullWidthNumConvert("Or in the middle of other text: 123. Thank you."));
Using String Normalize MDN with "NFKC" as the Unicode Normalization Form
const str = "150721";
console.log(str.normalize('NFKC')); // 150721
https://www.unicode.org/charts/normalization/chart_Number-Decimal.html

Split a given string and get last split value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a string like
count-contribute-1
count-contribute-11
count-contribute-1111
Here I want to split the string and get the last split value (i.e 1 , 11, 1111);
How can I do it?
split() on - and pop() of the last value
string.split('-').pop()
Use .pop() to get the last item from the array created by .split()
"count-contribute-1".split('-').pop();
Also you can get last part of numbers using regular expression. Like this:
s = "count-contribute-111"
s.match(/\d+$/)
//return "111"
It doesn't matter what separator you use.
s = "count-contribute-+*%111"
s.match(/\d+$/)
//return "111"

how to get length of substring in Regular Expression JavaScript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
here is my regular expression
/^[a-zA-Z0-9-]{40}([a-zA-Z0-9-]{3}2[a-zA-Z0-9-]{12}){2,10}$/
There will be one condition: the length of every string after first 40 characters must be 16 characters. This 16 characters string will be non repeating and minimum of 2 times and maximum of 10 times. So i want to get the length of this sub-string which should be 16.
Here is input string:
string input="PR212D4EB2-6B25-4020-BD2A-941F69C0AA4102GEX2421262506027GEX2437345435522"
I want to match the length of "GEX2421262506027" and "GEX2437345435522". Like this, there will be 10 strings max. I want to validate if this length should be 16 characters.
try this
var pattern = /^[a-zA-Z0-9-]{40}([a-zA-Z0-9-]{3}2[a-zA-Z0-9-]{12}){2,10}$/;
var exp = text.match(pattern);
if (exp) {
alert(exp[0].length);
}
if you just want the last 16 characters:
var string = 'PR212D4EB2-6B25-4020-BD2A-941F69C0AA4102GEX2421262506027GEX2437345435522';
var matching = string.match(/^[a-zA-Z0-9-]{40}([a-zA-Z0-9-]{3}2[a-zA-Z0-9-]{12}){2,10}$/);
console.log(matching[1]);
console.log(matching[1].length);

Categories

Resources