Javascript regex to extract the string before the last backslash [duplicate] - javascript

This question already has answers here:
Regex for everything before last forward or backward slash
(3 answers)
Closed 9 years ago.
I am dealing with timezone's in Javascript and I need a regex that will extract everything, but the timezone name from it. For example, I have the timezone America/Argentina/Buenos_Aires. I want to extract the America/Argentina part with a regex. Currently I have this regex: tz.match(/.*?(?=\/|$)/i)[0] which extracts everything to the first backslash which works for most timezones (America/Los_Angeles), but not for all of them. How could I edit that regex so that it gets the string before the last value?

I'd personally suggest avoiding regular expressions for something like this, when simple string functions/methods would suffice admirably:
var stringVariable = 'America/Argentina/Buenos_Aires',
text = stringVariable.substring(0, stringVariable.lastIndexOf('/'));

Related

How can I use Regular Expression to match Chinese characters like ,。‘;’ [duplicate]

This question already has answers here:
Regular expression to match non-ASCII characters?
(8 answers)
Closed 5 years ago.
I'm using javascript to convert string to what I want!!
Can I use Regular Expression and use what Regular Expression??
Is there anyone that can help me?
First, you need to get corresponding Unicode code for these Chinese characters. Here is a helpful tool.
character code(base 10) code(hex)
, 65307 0xff1b
。 65292 0xff0c
; 12290 0x3002
Second, use these Unicode code to form regexp.
js:
/[\u3002\uff0c\uff1b]/.test('A string contains Chinese character。')
true

Javascript Regex Not Giving Multiple Results [duplicate]

This question already has answers here:
How to match multiple occurrences of a substring
(3 answers)
Closed 5 years ago.
I am trying to read a string formatted like
<test>input</test>\n <another>input</another>
My regex works for the test tagged input, but ignores the another tagged input. If I wrap the entire regex in parenthesis and use the brackets {} to specify how many times, then it only saves the last match case. How can I catch and save all match cases?
My regex:
/([\n\s]*<([^>]+)>([^<>]*)<([^>]+)>[\n\s]*){0,}/
Result contents of match:
<test>input</test>\n <another>input</another>
<another>input</another>
another
input
/input
Add a g Modifier so specify that it is global (allows for multiple results)
So change your regexp to (notice the g in the end)
/([\n\s]*<([^>]+)>([^<>]*)<([^>]+)>[\n\s]*){0,}/g

Javascript remove extra underscores at the end of a string only [duplicate]

This question already has answers here:
Trim specific character from a string
(22 answers)
Closed 6 years ago.
I have a requirement to remove extra underscores from a string. The condition is if they occur at the very end of the string only.
As an example, we have DELL_ and DELL__ that needs to be changed to DELL.
I was considering using str.replace but I need to match cases specifically if it occurs at the end of the string and not all occurrences in that string. Also, I only want to run this script IF it detects the extra underscores.
I need to have some logic such as IF ( hasExtraUnderscores ) { remove extra underscores }
How can I do this in javascript?
NOTE: We are unable to use JQuery and need to do this in native javascript if possible.
Try this
var str = 'DELL_'
alert(str.replace(/_+$/,'');

Convert string into Regular Expression in Javascript [duplicate]

This question already has answers here:
Building regexp from JS variables not working
(5 answers)
Closed 7 years ago.
From the backend of my application, I receive a regular expression which should be matched with a postal code in the frontend.
However, every time I convert to string into a regular expression using the RegExp class, I get another regular expression which doesn't match my postal code anymore.
This is the code I'm currently using (copy from my console):
var str = '/^[1-9][0-9]{3}\s?([a-zA-Z]{2})?$/',
exp = new RegExp(str);
// Returns null
'1055AA'.match(exp);
// The code below does work though...
// Returns: ["1055AA", "AA"]
'1055AA'.match(/^[1-9][0-9]{3}\s?([a-zA-Z]{2})?$/);
Can someone help me solve this problem? Thanks!
Your input string must not begin and end with the Regexp markers / - after all, it's a regular string, not a literal regexp. Also, since it's a regular string (and not (yet) a regexp), you need to double the backslashes as usual in a regular string.

Javascript Regular Expression to match six-digit number [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I am trying to incorporate a regular expression i have used in the past in a different manner into some validation checking through JavaScript.
The following is my script:
var regOrderNo = new RegExp("\d{6}");
var order_no = $("input[name='txtordernumber']").val();
alert(regOrderNo.test(order_no));
Why would this not come back with true if the txtordernumber text box value was a six digit number or more?
You have to escape your \ when used inside a string.
new RegExp("\\d{6}");
or
/\d{6}/
Insert an extra "\" in your regexp.
You need to escape your backslash. It's looking for "\d", not digits.
So...
var regOrderNo = new RegExp("\\d{6}");

Categories

Resources