I am writing a google apps script that pulls the content from a CSV file in a gmail attachment. I need to split the csv data into several an array (with each row being it's own array). I've got all that down.
My problem is this: One of the values in the CSV is a "City, St" format. So when I split the array using string.split(',') I end up with an extra "column." My idea to fix this was to back up and kill that comma in the initial string. Here's the relevant portion of my code:
var attachments = msgs[i][j].getAttachments();
for (var k = 0; k < attachments.length; k++) {
var attachmentData = attachments[k].getDataAsString();
var regex = new RegExp('\,\s(?:[A-Z]{2}\")', 'gi');
attachmentData.replace(regex,' ');
...
So what I'm trying to do is just find a comma, followed by a space, followed by exactly two letters, then a quotation mark. I want to just replace the comma with a space. I've also tried
var regex = new RegExp('(\,\s)([A-Z]{2}\")', 'gi');
attachmentData.replace(regex,$2);
with no luck. Here's a random sample of the (very long) data string I'm running this on:
Voice,Incoming,(###) ###-####,(###) ###-####,,,,"Dallas, TX",12/12/2014,06:26 PM,Phone Call,Voicemail,00:00:27,$0.000,-,,
,Incoming,(###) ###-####,,###,,,"Dallas, TX",12/12/2014,06:26 PM,Phone Call,Voicemail,00:00
Can anyone see what I'm not seeing as to why this isn't working? (Or have any ideas of a better way to do this?)
The replace() method returns a new string with some or all matches
of a pattern replaced by a replacement.
The str.replace does not change the string, where as returns a new string with the replace. Hence you may want to write something like
var regex = new RegExp('(,\\s)([A-Z]{2}")', 'gi');
var replacedData = attachmentData.replace(regex,'$2');
Note
You can drop the first capture group as
var regex = new RegExp(',\\s([A-Z]{2}")', 'gi');
var replacedData = attachmentData.replace(regex,'$1');
You can make use of a regex with this condition and then print back the block with $1 together with the space:
s = s.replace(/,( [A-Z]{2}")/, ' $1');
^^ ^ ^^^
comma ^^^^^^^^^ print back replacing comma with space
|
catch the group of space + two letters + "
See it live:
var s = 'Voice,Incoming,(###) ###-####,(###) ###-####,,,,"Dallas, TX",12/12/2014,06:26 PM,Phone Call,Voicemail,00:00:27,$0.000,-,, ,Incoming,(###) ###-####,,###,,,"Dallas, TX",12/12/2014,06:26 PM,Phone Call,Voicemail,00:00';
s = s.replace(/,( [A-Z]{2})/, ' $1');
document.write(s)
Related
I'm trying to remove all the text which falls before the last character in a Regex pattern.
Example:
rom.com/run/login.php
Becomes:
login.php
How would I go about doing this in JavaScript? I'm new to regular expressions.
To get everything after last slash, use [^\/]+$
const str = "rom.com/run/login.php";
console.log(str.match(/[^/]+$/)[0]);
You can get the result you need by searching for a literal string (just one character in fact) so there's no need to employ regular expressions which will cost you performance.
You can split the input into chunks separated by / and get the last chunk:
var input = 'rom.com/run/login.php';
var result = input.split('/').pop();
Or find the position of the last occurrence of / in the input, and get the remainder of the string that follows that position:
var input = 'rom.com/run/login.php';
var result = input.substring(input.lastIndexOf('/') + 1);
One approach is a regex replacement:
var path = "rom.com/run/login.php";
var output = path.replace(/^.*\//, "");
console.log(output);
The regex pattern ^.*/ is greedy, and will consume everything up to (and including) the last path separator. Then, we replace this match with empty string, to effectively remove it.
You could do it with Regex like this:
var url = 'rom.com/run/login.php'
var page = url.match('^.*/(.*)')[1]
console.log(page)
Or you could do it without Regex like this:
var url = 'rom.com/run/login.php'
var split = url.split('/')
var page = split[split.length-1]
console.log(page)
I'm in non-modern JavaScript and I have a string defined as follows:
"//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0"
I want to pull out just the DmYK479EpQc but I don't know the length. I do know that I want what is after the / and before the ?
Is there some simple lines of JavaScript that would solve this?
Use the URL object?
console.log(
(new URL("//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0", location.href)).pathname
.split('/')
.pop());
Why? Because I can likely make up a URL that defeats the regex (though for youtube it's probably unlikely)
This expression might help you to do so, and it might be faster:
(d\/)([A-z0-9]+)(\?)
Graph
This graph shows how the expression would work and you can visualize other expressions in this link:
const regex = /(.*)(d\/)([A-z0-9]+)(\?)(.*)/gm;
const str = `//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0`;
const subst = `$3`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
Performance Test
This JavaScript snippet shows the performance of that expression using a simple 1-million times for loop.
const repeat = 1000000;
const start = Date.now();
for (var i = repeat; i >= 0; i--) {
const string = '//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0';
const regex = /(.*)(d\/)([A-z0-9]+)(\?)(.*)/gm;
var match = string.replace(regex, "$3");
}
const end = Date.now() - start;
console.log("YAAAY! \"" + match + "\" is a match 💚💚💚 ");
console.log(end / 1000 + " is the runtime of " + repeat + " times benchmark test. 😳 ");
How about non-regex way
console.log("//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0".split('/').pop().split('?')[0]);
I'm not going to give a piece of code because this is a relatively simple algorithm, and easy to implement.
Please note that those links has this format (correct me if I'm wrong):
https:// or http://
www.youtube.com/
embed/
Video ID (DmYK479EpQc in this case)
?parameters (note that they start ALWAYS with the character ?)
You want the ID of the video, so you can split the string into those sections and if you store those sections in one array you can be sure that the ID is at the 3rd position.
One example of how that array would look like would be:
['https://', 'www.youtube.com', 'embed', 'DmYK479EpQc', '?vq=hd720&rel=0']
One option uses a regex replacement:
var url = "//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0";
var path = url.replace(/.*\/([^?]+).*/, "$1");
console.log(path);
The above regex pattern says to:
.* match and consume everything up to and
/ including the last path separator
([^?]+) then match and capture any number of non ? characters
.* then consume the rest of the input
Then, we just replace with the first capture group, which corresponds to the text after the final path separator, but before the start of the query string, should the URL have one.
You can use this regex
.* match and consume everything up to
[A-z0-9]+ then match and capture any number and character between A-z
.* then consume the rest of the input
const ytUrl = '//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0';
const regex = /(.*)(d\/)([A-z0-9]+)(\?)(.*)/gm;
const position = '$3';
let result = ytUrl.replace(regex, position);
console.log('YouTube ID: ', result);
This regex just split the string into different sections and the YouTube id is at the 3rd position.
Another, solution is using split. This method splits a string into an array of substrings.
const ytUrl = '//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0';
let result = ytUrl.split('/').pop().split('?').shift()
console.log('YouTube ID: ', result);
In this sample, we split the URL using / as separator. Then we took the last element of the array with the pop method. and finally we split again using ? as separator and we take the first element of the array with the shift method.
I have one string as
str="NA,No Therapis delivered,No Therapies Available,None,ATP,CVRT,CVRT x 2,No VT Available,Aborted CVRT,Aborted Defib,Defib,Defib(DBT)"
I want a regular expression that would match comma seperated values.
I am using Datatable to show above string in a table.
e.g If i enter 'cvrt' then only 'cvrt' from above strin should be returned.
If I enter 'No Therapis delivered' then only 'No Therapis delivered' should be return.
As I want to do Datatable search, split() method wont work for me.
Only option is to use regular expression.
Thanks in Advance
You could try something like so: (^|,)No Therapies Available(,|$). This will look for a particular word, in the case, No Therapies Available which is preceded by either by the beginning of the string (^) or a comma (,) and is succeeded either by another comma (,) or the end of the string ($).
As per this previous SO question, you could use exec to match and obtain the location of your search results.
EDIT: The code would look something like so:
var searchText = ...;
var searchableText = ...;
var match = [];
var regex = "(^|,)(" + searchText + ")(,|$)/g"
while(match = regex.exec(searchableText)) != null)
{
alert("Item found at " + match[1].index);
}
EDIT 2:
The comma's will be part of the match since those are part of the pattern. My updated answer uses groups to go round this problem, which it would seem is not something you have access to. To go round this problem, you could do something like so:
var matchedText = $('#example').dataTable().fnFilter( "(^|,)" + searchText + "(,|$)/g",columnIndex,true); //this should return a string of the form ',foobar,`
//We then proceed to clean the string by removing leading/trailing commas:
matchedText = matchedText.replace("^,", ""); //Remove leading commas.
matchedText = matchedText.replace(",$", ""); //Remove trailing commas.
alert("Matched Text: " + matchedText);
I have written this in javascript (web) but since i try to use RegExpression it should work almost the same.
I have a string with some coordinates in it seperated by a space charakter.
var coords = "0:0 0:0:0 1:0:1 0:0:0:1";
var part = "0:0";
I want to have all the coordinates beginning with the value of part ("0:0");
What I tryed is something like:
var reg = new RegExp(part+"*");
alert(coords .match(reg));
But it seems not to work propperly.
It should match "0:0" and "0:0:0" and "0:0:0:1" but NOT "1:0:1" (edit)
Anyone has an idea?!
Kind regards!
You should use this regex:
var reg = new RegExp("(^|\\s)(" + part + "\\S*)", "g");
that is to match all non-space characters after 0:0 and stop when it hits a space or line end.
I'm trying to search for '[EN]' in the string 'Nationality [EN] [ESP]', I want to remove this from the string so I'm using a replace method, code examaple below
var str = 'Nationality [EN] [ESP]';
var find = "[EN]";
var regex = new RegExp(find, "g");
alert(str.replace(regex, ''));
Since [EN] is identified as a character set this will output the string 'Nationality [] [ESP]' but I want to remove the square brackets aswell. I thought that I could escape them using \ but it didn't work
Any advice would be much appreciated
Try setting your regex this way:
var regex = /\[EN\]/g;
If you just want to replace a single instance of it you can just str = str.replace("[EN] ", ""); otherwise; var find = "\\[EN\\]";.