i have a html page , i use regex to remove all html tags from the page and extract the text using the below code.
var foo = loader.data.replace(/<.*?>/g, "");
var bar:Array = foo.split("Total");
foo = foo.split(bar[0]);
trace(foo);
And using the same code lines below the replace method i remove every string before the word "TOTAL". It does the job perfectly but now i want to apply and other split to get contents after "TOTAL" and remove the Content after "BYTES".
So when i try to split it up again with
var bar2:Array = foo.split("BYTES");
foo = foo.split(bar2[0]);
Flash returns a error saying SPLIT is a not a valid method :S
I tried several other ways , ( REPLACE ) but still flash produces errors.
Can Anyone help me to get through this ?
Thank you
".split()" is a method of String. When you did the assignment below:
foo = foo.split(bar[0]);
foo became an array, and thus the call
var bar2:Array = foo.split("BYTES");
was being made to an array, which is invalid (no such method)
What you want instead is this:
var foo = loader.data.replace(/<.*?>/g, "");
trace(foo);
var result = foo.split("Total")[1].split("BYTES")[0];
trace(result);
Related
I have a function that stripes HTML markup to display inside of a text element.
stripChar: function stripChar(string) {
string = string.replace(/<\/?[^>]+(>|$)/g, "")
string = string.trim()
string = string.replace(/(\n{2,})/gm,"\n\n");
string = string.replace(/…/g,"...")
string = string.replace(/ /g,"")
let changeencode = entities.decode(string);
return changeencode;
}
This has worked great for me, but I have a new requirement and Im struggle to work out where I should start refactoring the code above. I still need to stripe out the above, but I have 2 exceptions;
List items, <ul><li>, I need to handle these so that they still appear as a bullet point
Hyperlinks, I want to use the react-native-hyperlink, so I need to leave intack the <a> for me to handle separately
Whilst the function is great for generalise tag replacement, its less flexible for my needs above.
You may use
stripChar: function stripChar(string) {
string = string.replace(/ |<(?!\/?(?:li|ul|a)\b)\/?[^>]+(?:>|$)/g, "");
string = string.trim();
string = string.replace(/\n{2,}/g,"\n\n");
string = string.replace(/…/g,"...")
let changeencode = entities.decode(string);
return changeencode;
}
The main changes:
.replace(/ /g,"") is moved to the first replace
The first replace is now used with a new regex pattern where the li, ul and a tags are excluded from the matches using a negative lookahead (?!\/?(?:li|ul|a)\b).
See the updated regex demo here.
My sheet is a query-sheet from database. Some of them contain html hex-code color which I need to manually use edit>Find and Replace every time it is refreshed.
I am very new to Google App Script and been trying to use the following code:
function Clearcode() {
var lookupone = new RegExp(/{color:#.{7}/);
var rep = "";
var spreadSheet = SpreadsheetApp.getActive();
var querySheet = spreadSheet.getSheetByName("QUERY");
var lastRow = querySheet.getLastRow();
var lastColumn = querySheet.getLastColumn();
var data = querySheet.getRange(2, 1, lastRow-1, lastColumn).getValues();
var textfinder = querySheet.createTextFinder(lookupone);
var found = textfinder.replaceAllWith(rep);
return (found);
}
Yet, when I run this function in the sheet it did not work. Any thought?
P.S. I planned to eliminated "[color]" part of the hex-code as well by create the similar function.
P.S.2 I have attached a snapshot of a table as you requested. The red line is just for confidentiality of the data. Below the line is just a normal text.
Pay attention to types!
CreateTextFinder accepts String as argument NOT a regexp object.
To use strings as regular expressions, useRegularExpressions needs to be set to true
querySheet.createTextFinder("\\{color:#?.{0,6}\\}")//only 6 characters
.useRegularExpressions(true)
.replaceAllWith("")
I have an automatically generated string which looks as follows:
["January","February",null,"April"]
I need to remove any match of ",null" from the string, ie:
["January","February",null,"April"] --> ["January","February","April"]
How can I find everything except for ",null"?
I have tried variations of "^(?!,null).*" without success.
To answer your question as stated, you don't need regex:
str = str.replace(",null", "");
However, to handle the edge cases too:
["January","February",null,"April"] --> ["January","February","April"]
["January",null,null,"April"] --> ["January","April"]
[null,"January","February","April"] --> ["January","February","April"]
["January","February","April",null] --> ["January","February","April"]
[null] --> []
you would be better served with regex:
str = str.replaceAll("(?<=\\[)null,?|,null", "");
The replacement regex caters for null the first (and potentially only) position, and any other case.
Please review this one, we can use filter function
var arr = [null, "January","February",null,"April", null];
var arr2 = arr.filter(function(x){return x !== null})
console.log(arr,arr2);
If I have a Tridion URI like this 'tcm:1-23-8' and I want to get 23 with a Regular Expression.
The following works, but I know there is a better way. tcm: and '-8' are always there. The parts that change are 1 and 23.
var schemaUri = $display.getItem().getId(); // tcm:1-23-8
var re = /tcm:\d-/gi; // = 23-8
var schemaIdWithItemType = schemaUri.replace(re, "");
re = /-8/gi;
var schemaId = schemaIdWithItemType.replace(re, "");
If the number is always between the 2 dashes, you could do this:
var schemaId = schemaUri.split('-')[1];
This does the following:
split the string on the '-' character --> ['tcm:1', '23', '8'];
Get the second item from that array, '23'
Or, try this:
var schemaId = schemaUri.match(/-\d+-/)[0].replace(/-/g,'');
This'll find the number in between the dashes with .match(/-\d+-/), then remove the dashes.
Rather than calling $display.getItem().getId();, you can just call $display.getUri(); and then use the split()
var schemaId = $display.getUri().split('-')[1];
If you did want a pure Regex solution...
/^tcm:(\d+)-(\d+)(?:-(\d+))?$/i
Should validate your Tridion URI's format and provide you with 3 submatches, the second of which will be the Item ID
I'm trying to remove beggining numbers from a column in a Google Docs spreadsheet using regex. I can't get RegExReplace function to work. This is the error I get when I run/debug the code:
Missing ) after argument list. (line 14)
This is a part of my code (line 14 is the RegExReplace function line, bolded):
regexFormat = "^[0-9]+$";
replVal = value.RegExReplace(value; regexFormat; ""); //error here
rplc.setValue(replVal);
This is the official syntax: RegExReplace( text ; regular_expression ; replacement )
Anyone knows how to use this function? Thanks!
I don't know why the docs list a semicolon, but if you are doing it as a spreadsheet function, you still need to use commas. Try the following:
=REGEXREPLACE("What-A Crazy str3ng", "\W", "")
Which as expected, yields
WhatACrazystr3ng
I've found another solution for replacing with regexp in Google Docs Script:
var replace = '';//because we want to remove matching text
var regexp2 = new RegExp("[0-9]*[\.]*");//an example of regexp to do the job
var valcurat = value.replace(regexp2, replace);//working
As I did not find any solution for RegExReplace, I changed the method with replace(regexp, new_text). This one works.
This is just a guess but if the function is Javaish, maybe there are 2 forms.
Form 1:
myvar = RegExReplace(value; regexFormat; "");
Form2:
myvar = value.RegExReplace(regexFormat; "");