Line break after every 4 consecutive numbers in string - javascript

I have a string that looks like this:
"user modified avdd on december 1989user created avdd on august 21th, 2010"
I would like to append a new line after every four digits, so the end result will be:
"user modified avdd on december 1989
user created avdd on august 21th, 2010"
I already know that \/d/d/d/d\ will match those numbers in regex.
I tried doing it this way:
var rows = "user modified avdd on december 1989user created avdd on august 21th, 2010";
var logs = row.replace(\/d/d/d/d\,"\n");
(and then looping the .replace) but the result omits the 4 digits from the result.
Can i replace without ommiting ? is there a better approach ?
EDIT: as suggested i tried .replace(/\d{4}/, "\n") problem is this output removes the numbers from the final string:
so im getting
"user modified avdd on december
user created avdd on august 21th, 2010"
instead of the desired output:
"user modified avdd on december 1989
user created avdd on august 21th, 2010"

U need to use the feature called "Capturing Group".
Parenthesis around a section of a regex allows you to capture it and append it in the replaced string with $1, $2, etc.
This function does the trick :
var logs = rows.replace(/(\d{4})/g,'$1\n');
(\d{4}) = captures an occurence of 4 digits;
/g = global parameter to replace all occurences ( so no need to loop after the execution )
By the way, on a robustness point of view, you need to be SURE that a user cant have 4 successive digits in his id !

Related

how to do regex date format change?

Currently below regex is working fine with dates but I want it to accept those date and month also which has single digit. How can I do that?
Regex should accept below formats also:
'11/4/2021' or '1/4/2021' or '1/04/2021'
dateString = '11/04/2021'
let dateformat = /^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([1][26]|[2468][048]|[3579][26])00))))$/g;
if(dateString.match(dateformat)){
let operator = dateString.split('/');
console.log(operator)
}
Don't. Use Date.parse() instead.
For month: ^0{1}[1-9]$|^[1-9]{1}$|^1[012]{1}$
The first part is with 0, the second part is without 0, and the last one is for 10, 11 and 12.
For days: ^0{1}[1-9]{1}$|^[1-9]{1}$|^[12]{1}[0-9]{1}$|^3[01]{1}$
The first one is for days with from 1-9 starting with 0 and the second one is for the same but without the 0.
About the if the max day is 31, 30 or 28 I would use javascript for that.
You are able to achieve the requested by adding to the 8th group an OR statement with the 1 to 9 characters. That's for the days. The same goes for the months.
Let me give you an example.
Your matching group for the days right now is looking like this:
(0[1-9]|[12]\d|30)
Which means that you accept all numbers which start with 0 and a digit from 1 to 9 afterwards, a number starting with either 1 or 2 and any digit afterwards, or 30.
In order to accept the digits from 1 to 9 you have to add another condition to your matching group and that is the 1 to 9 digits. So your group will look something like the following:
(0[1-9]|[12]\d|30|[1-9])
This is the most basic thing you can do. There are plenty of ways to optimize this regex and do it in a better way. You can think about the 31st day of the month, since right now it is not capturing it.
The same way I shown in the example for the days' matching group, you can do it for the months' matching group.

How to add a newline every three character in javascript

How can i split a string that contains | (pipe) as separator lets after every 3 pipes?
I'll explain better:
i have a string that results in dates, something like this:
Tuesday - 23-06-2015| Wednesday - 24-06-2015| Thursday - 25-06-2015| Friday - 26-06-2015| Monday - 29-06-2015| Tuesday - 30-06-2015|
And i'd like to add a <br/> so that i have only 3 dates on the same line, like this:
Tuesday - 23-06-2015| Wednesday - 24-06-2015| Thursday - 25-06-2015|
Friday - 26-06-2015| Monday - 29-06-2015| Tuesday - 30-06-2015|
I found this code to count the times the pipe is repeated in the string:
var CustomerDatesSplitted = (customerDates.match(/\|/g) || []).length;
console.log(CustomerDatesSplitted ); //Logs 6, the right value
and i found this code that needs a specific value to insert in the regex (in this case, adds a new line after 10 charactes:
customerDates.match(/.{1,10}/g).join("<br/>");
but i don't know the best way to make it "dynamic" so that it checks only the pipes and split after the 3rd, 6th, 9th and so on...
suggestions?
You can do:
str = str.replace(/((?:[^|]*\|){3})/g, '$1<br>');
RegEx Demo

Regex for timestamp string

I'm looking to target the following string using jQuery. Specifically, I need to wrap it in a strong tag for styling purposes. I can't change the source data. My regex-fu is pathetic. Any suggestions?
Nov 18, 2013, 4pm CST:
Thanks guys - these are excellent answers. I should have been slightly more specific - I need to match all occurrences of this format within a collection, e.g.:
$('.admin-comments').match(/[A-Z]{1}[a-z]{2}\s[0-9]{1,2},\s[0-9]{4},\s[0-9]{1,2}[a|p]m\s[A-Z]{3}/)
(I have a log of comments and I'm trying to wrap the timestamp in a strong element.)
Edit: Final Working Solution
var adminComment = $('.admin-comments');
if (adminComment.length) {
var adminCommentTxt = adminComment.text();
var formatCommentTimestamp = adminCommentTxt.replace(/([A-Z]{1}[a-z]{2}\s[0-9]{1,2},\s[0-9]{4},\s[0-9\s]{1,2}[ap]m\s[A-Z]{3}\:)/g, "<strong>$1</strong>");
adminComment.html(formatCommentTimestamp);
}
Here you go: /^[A-Z]{1}[a-z]{2}\s[0-9]{1,2},\s[0-9]{4},\s[0-9]{1,2}[a|p]m\s[A-Z]{3}\:$/
'Nov 18, 2013, 4pm CST'.match(/^[A-Z]{1}[a-z]{2}\s[0-9]{1,2},\s[0-9]{4},\s[0-9]{1,2}[a|p]m\s[A-Z]{3}\:$/)
["Nov 18, 2013, 4pm CST"]
Keep in mind that this regex is expecting the line to start and end with this date, if the date is contained within other text, remove the ^ from the start and the $ from the end.
Hope this helps.
To further explain the regex and hopefully ++ your "regex-fu"
[A-Z]{1} - match one upper case letter
[a-z]{2} - match two lower case letters
So far we are at Nov, Oct, Jan, etc.
\s - space
[0-9]{1,2} - a 1 (min) or 2 (max) digit number
, - literal comma
\s - space
[0-9]{4} - a 4 digit number (the year)
So now we have matched: Nov 18, 2013
, - literal comma
\s - space
[0-9]{1,2} - just like before, a one or two digit number
[a|p]m - 'a' or 'p' followed by an 'm'
Now we've matched: Nov 18, 2013, 4pm
[A-Z]{3} An upper case three character string
\: literal colon
That is the entire string.
Putting ^ at the beginning of the regex means the text we are matching against MUST begin with the pattern; similarly, the $ states that the text we are matching MUST end with the pattern.
Good luck!
Rob M's answer is perfectly valid, and a more generic version than mine and may be exactly what you're looking for. However, if you want to be more specific with your months and time zones this may be useful for you:
(Oct)?(Nov)?\s\d{1,2},\s\d{4},\s\d{1,2}(pm)?(am)?\s(CST)?(PST)?:
This will match all of:
Nov 18, 2013, 4pm CST:
Oct 8, 2011, 11am PST:
Nov 02, 1981, 2am PST:
Oct 31, 1843, 12pm CST:
If you needed more months, you simply add each one like so:
(Mmm)? where Mmm corresponds to the month you want to match.
Similarly if you need more time zones, you'd add them like so:
(ZZZ)? where ZZZ corresponds to the timezone you want to match.
Similar to Rob's answer, if your date string is the only thing on the line, you could add the /^ and &/ prefix & suffix.

Regex difficulties in google mail app script - select word between two words

The script reads the plain text from emails and sends them to another function to write exact values in a spreadsheet.
My problem is to get some data(date, name, etc) from email plain text.
Here is the function:
var threads = GmailApp.search('label:aoaoaoaoa');
Logger.log(threads);
Logger.log(threads[0].getMessages());
Logger.log(threads[0].getMessages().length);
var messages;// = threads[0].getMessages();
var ContentEmail;// = messages[0].getBody();
var bodyRe = new RegExp("(?=start date)(.*)(?=finish date)");
for (var i = 0; i < threads.length; i++) {
messages = threads[i].getMessages()
ContentEmail = messages[0].getPlainBody();
var myArray = bodyRe.exec(ContentEmail);
Logger.log(myArray);
myArray is null.
I tried many regex notations to get text between two words. Some of them return null. The others caused SyntaxError in the script.
This is an example message that I want to get date, name, tel number, email and place.
Confirmation Code: CAXNA3JAA Tue, October 01, 2013
start date Tue, October 08, 2013
finish date
Place: AOA Studio <http://www.**********************>
[image: Ali] <http://www.**********************>
Ali <http://www.***************************>
+49 000 000 00000
******#gmail.com
Or do you suggest to use google script html functions to read data from emails? Here
If you can specify using multiline you could use the following:
^start date\s*(.+)
However this doesn't include that find date shows up next.
Otherwise, this worked for me: (you may want to be careful with line breaks)
start date\s*(.+)\nfinish date
To include \r or \n you can use:
start date\s*(.+)(\r|\n)+finish date

regex javascript to add to expression

I have to write a few regexes. I did all except the following. I don't know how to add something via regex only.
Change this  9/28/2005  to  09.28.2005  (Am able to replace slashes with period but how to add leading zero to 9)
Change September 21, 2006 to 21. September 2006 (am able to replace , with . but how to change the order?)
Change 5:00 PM to i7:00 Uhr   (12 to 24 hour)?
1st Case (padding with zero):
result = subject.replace(/\b(\d)(?=[\/.])/ig, "0$1");
2nd Case (change order):
result = subject.replace(/\b(january|february|march|april|may|june|july|august|september|october|november|december) +(\d{2}), +(\d{2,4})\b/ig, "$2. $1 $3");
3rd case (12-24 hr)
As JavaScript RegEx engine does not support If-Then-Else Conditionals. So, it is hard and time-taking job to create a pure RegEx pattern for that purpose.
Hope this helps.
Just do the math manually... e.g. make a function GetTimeZoneOffset and go from there...
Return the timezone difference between UTC and Local Time:
var d = new Date()
var n = d.getTimezoneOffset();
The resulting value of n will be: 240
Add the difference and viola you have a real date which you can format what way you want..

Categories

Resources