I tried to put HTML acquired from a web page with GAS into a variablesource
and assign multiple data to an array with.match
.
I don't know how to write a regular expression.
<p>10 days</p>
: Omitted
<p>18 days</p>
: Omitted
<p>20 days</p>
Assuming that there is the above part in the HTML of the web page, I would like to extract the** day
part of this and assign it to the array.
How should I write the regular expression in this case?
I tried various things, but I couldn't extract it from the whole page becausejss ***
was unspecified.
The numerical part ofjss ***
is fixed as 3 digits.
HTML is assigned tosource
with the following code.
const URL = 'https://example.com';// Target URL
var key = '##-#####-#####-#####-#####-#####';
var option =
{url: URL,
renderType: "HTML",
outputAsJson: true};
var payload = JSON.stringify (option);
payload = encodeURIComponent (payload);
var url = "https://phantomjscloud.com/api/browser/v2/"+ key +" /? request = "+ payload;
var response = UrlFetchApp.fetch (url);
var json = JSON.parse (response.getContentText ());
var source = json ["content"] ["data"];
The other parts are a little brute force, but the other parts could be extracted as follows.
var itemRegexp = new RegExp (/ /g);
var item = source.match (itemRegexp);
for (var i = 0;i<item.length;i ++) {
item [i] = item [i] .replace (" "," ");
Logger.log (item [i]);
}
Thank you
Addition (2019/11/27)It seems that it was difficult to understand the execution environment.
Execution environmentGoogle Apps Script
Since this script file is written in ES5, I want to use ES5 basically.
-
Answer # 1
-
Answer # 2
Tried to assign multiple data to an array
If you are interested in arraying, you can also specify querySelectorAll () argument with attribute selector.
The next three implement matching functions like regular expressions.
[
Attribute name^ =
Value]
[
Attribute name$=
Value]
[
Attribute name* =
Value]
If you include the
jss
of your question, you can get it withp [class * = jss]
.Assuming that there is the above part in the HTML of the web page, I would like to extract the ** day part from this and assign it to the array.
How should I write the regular expression in this case?let paragraphs = document.querySelectorAll ("p [class * = jss]"); let days = Array.from (paragraphs) .map (el =>el.textContent); console.log (days): // ["10 days", "18 days", "20 days"]
Addendum)
This is an example of handling DOM without acquiring HTML source.
Related articles
- javascript - i want to process an array of objects obtained from firestore with actions with a for statement with mutaions
- javascript - i want to make an array of the elements obtained by clicking
- javascript - [typescript] i can't write the type definition of an array with a variable length at the front
- javascript - i want to get an array of appointments from a calendar with gas and output them to a spreadsheet every month
- probably a very simple way to write additional javascript (jquery) elements
- please tell me how to write super basic javascript
- javascript - getting a value from an array of js makes it undefined
- javascript regular expression
- javascript - i want to write a code that puts out a leap year in js
- javascript - i can only write imperatively
- javascript - i suddenly stumbled on a regular expression
- php - i want to know how to write a regular expression for a symbol when preg_matching
- javascript - i want to automatically create an array with csv
- javascript - i want to get the value associated with the array key in the object
- rewrite nested associative array values in javascript
- how to amplify an array with javascript
- javascript - i want to get an element from a multidimensional array
- javascript: about output of multidimensional array
- javascript - get the value of the array in the object , how to use in an if statement
- javascript - i want to display the contents of an associative array and a combination of arrays
- javascript - i want to get an array of appointments from a calendar with gas and output them to a spreadsheet every month
- javascript - i want to share a spreadsheet link with gas error details: i want to resolve access denied
- javascript - when i update a value with glide, i want to detect the update with gas and add the value to another sheet
If you can use matchAll, you can write ...
Is it the following because it will not be used yet? I think that this part of the explanation of exec will be helpful.
This can also be achieved by passing the replacer function as the second argument of replace and doing the following: