Replace in html

Hi How i can use this code to replace the var data=[ in the html code

Dim html As String = Replace(kChartDrawHTML, “%ChartDrawJavaScript%”, kChartDrawJavaScript)

Self.LoadPage(html)


Chart Test

What is it that you are trying to replace ?

var Data=[…]

You could do that for example with:
Dim HTMLParts() as String = HTML.Split(“var data = [”)
If HTMLParts.ubound>0 Then HTMLParts(1)=HTMLParts(1).Right(HTMLParts(1).Len-HTMLParts(1).Instr("]"))
HTML=Join(HTMLParts,Replacement)

Use Regex to get the string you want replaced, and after that do a simple Replace. Simple.

how i can generate the all html from text file.

i think is easy

What’s wrong with ReplaceAll here?

So who you replace all data in var Data=[…]

I assumed that he also wants to replace the parameters which are unknown at the time of writing. In which case you don’t have a predefined text to replace.

[quote=125631:@Alexis Colon Lugo]how i can generate the all html from text file.

i think is easy[/quote]
Not sure what you wish to do here…

Again
I split the html in a before and after the “Data=[” (assuming there can be only one occurence.)
Look in the remainder for the closing “]” and discard the part in front. Join them back together with your replacement text in the middle.

Ralf, what you described is exactly what you’d use a regular expression for. You’ll save yourself a ton of coding time with regular expressions.

Never really got the hang of regexp, can you give me an example how to do this with regexp ?

The pattern would be something like:

Data=\\[([^]]+)\\]

Let me break this down into parts for you:

Data=       # look for "Data="
\\[          # open square-bracket, literally
(           # start a capturing group
[^]]+       # a series of character that are *not* the close square-bracket
)           # close the capturing group
\\]          # closing square-bracket, literally

The Xojo code would be:

dim rx as new RegEx
rx.SearchPattern = "Data=\\[([^]]+)\\]"

dim match as RegExMatch = rx.Search( theText )
while match <> nil
  // do stuff
  match = rx.Search // Find the next one
wend

Honestly, it looks worse than it is, and is much easier to read in a dedicated editor like (ahem) RegExRX, but once you get the hang of them, they will change your programming life.

Excellent product :wink:

My first reflex is to go for ReplaceAll, but it quickly becomes a mess while sometimes a Regex can do that in one line. I just wish it took me less time to figure, even with the assistance of RegExRX ! Fortunately, there are already a lot of ready-made RegEx over the Internet for usual searches.

Since you’ve already built in a replacement for ChartDrawJavaScript, can’t you make data= a replacement, too? Would be easier to understand when you come back to it later.

Thank you very much, it was educational :wink:

Hi Kem look good but where i start using RegExRX

So if any Video Tutorial

I never like Web app but now is the people ask for small web app and y have to learn.

Thanks to in the Forums

No, no video tutorial but the app is fully functional except for a nag screen, so you can try it out. Within the Help menu, there is an option to Download Samples that will give you many (hopefully useful) examples of regular expressions.

http://www.mactechnologies.com/downloads

I should add that this is a great site to learn about regular expressions, and they offer a tutorial. ( have no relationship to them other than being a fan.)

I will check it out thanks :slight_smile: