Anyone know of any good classes or functions that will do this? I've found some regexes but what I need is to pass the string to a method and have it return the same string, but with urls turned blue and turned into hyperlinks. Seems like a fairly common task, but I can't find anything.
EDIT - the following works for any link starting with http:
var myPattern:RegExp = /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/i;
var str = text.replace(myPattern, "<font color='#04717D'><a target='_blank' href=\"$&\">$&</a></font>");
field.htmlText = str;
But it doesn't work for links that start with "www", because the href ends up looking like this:
www.google.com
Would love to know how to fix that.
I'm wary of making the existing regular expression/ replacement call any more complicated. With that in mind the most straightforward way of doing this is probably to write a second regular expression to correct any bad tags in the output from the first. I'd also add a 'g' to the end of your main regular expression so that it captures multiple URLs in the text
So, your main regular expression would now look like this:
var mainPattern:RegExp = /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/ig;
Your secondary regular expression will look something like this:
var secondaryPattern:RegExp = /\"www/g;
it should capture any links that don't start with "http:"
You then run both these expressions over your input string replacing as necessary:
var someText:String = "This is some text with a link in it www.stackoverflow.com and also another link http://www.stackoverflow.com/questions/5239966/as3-detect-urls-in-dynamic-text-and-make-them-links";
someText = someText.replace(mainPattern, "<a target='_blank' href=\"$&\">$&</a>");
someText = someText.replace(secondaryPattern, "\"http://www");
Related
Hello can anybody help me with trying to edit a field inside an ifram using CHROME.ahk
The following code works when I create an IE ojbect. This is the part I use to fill in the field.
workoder := test
frame := pwb.document.parentWindow.frames("uxtabiframe-1068-frame")
frame.document.GetElementsByName("ff_workordernum").item[0].Value := workorder
How do I do this using the chrome.ahk library.
and it doesn't work. I have tried several methods. Can anybody please help
I have trid this:
PageInstance.Evaluate("document.parentWindow.frames('uxtabiframe-1068-frame').document.GetElementsByName('ff_workordernum').item[0].Value = 'test' )
I get an error. I tried
iframeJs =
(
var iframe = document.getElementById('uxtabiframe-1068-frame');
var test2 = iframe.contentDocument..GetElementsByName('ff_workordernum').item[0].Value = 'test';
)
PageInstance.Evaluate(iframeJs)
I believe the problem with the first try you are trying to get the property "document" of the Iframe here:
frames('uxtabiframe-1068-frame').document
However Iframes have no such property, it's called "contentDocument"
I think this is silly and confusing that it is like this, just "document" makes more sense and should be the standard.
Also in the first try there is an opening double quote but there isn't a closing one.
In the second try there is a repeated "." here:
iframe.contentDocument..GetElementsByName
You only need one of them.
And also with the second try you've tried to assign a variable over multiple lines, everything must be on the same line and you can do that by using "`n" which is an escaped newline character.
I need to delete all text in html of body letter before one word from set of values ("Уважаемый", "Уважаемая", "Уважаемые").
I try to do like this:
LetterBody.Replace(Regex.Split(LetterBody, "(?=Уважаемый)|(?=Уважаемая)|(?=Уважаемые)")(0), "")
but using this method I lose almost all the formatting in the body of the email.
I programming in UiPath Studio using vb.net
Try using Substring and IndexOf.
Would look something like:
var myParameter = "Уважаемый";
var stuffINeed = LetterBody.Substring(LetterBody.IndexOf(myParameter) + myParameter.Length);
You could do this with the Invoke Code method or just use an Assign and replace the myParameter variable with the context you require.
The above will remove everything before the myParameter keyword you choose.
If you want to remove everything after it then you would add after the above:
var endResult = stuffINeed.Substring(0, stuffINeed.IndexOf(myParameter))
I'm using a HTML Entity to show three dots (...) to indicate uses that there was having more text. There were other ways to show this through CSS (text-overflow: ellipsis;), but I have some limitations to use CSS style.
The issue is, the ellipsis is showing in the middle of the text - I need it the same as how manual three dots appear after a text like,
Many people have sent language examples over the years...
My current output:
horizontal ellipsis
<p><span>Sample Text:</span>Many people have sent language examples over the years, but we want more! We'd love to add more examples…</p>
ToolJS is a Utility JavaScript library with functions that solve things like this and ease your work flow.
It contains a "Str" module that manipulates a string, and amongst them is the .truncate function which will give you want you need.
How to use it
Get the code either through its CDN or NPM and use as shown below
var Str = ToolJS.export("Str");
var myString = "Many people have sent language examples over the years, but we want more! We'd love to add more examples";
var newString = Str.truncate(myString, {
limit: 40, // this is the number of characters you want to show in your string
replacement: "..." // this is the replacement string
});
// Or you could do a shorthand implentation using the same method
var newString = Str.truncate(myString, 40, "...");
// To show it on your DOM
var DOM = ToolJS.export("DOM");
DOM.html("p", `<p><span>Sample Text:</span>${newString}</p>`);
<script src="https://unpkg.com/#redeakaa/tooljs#1.0.1/dist/umd/tooljs.min.js"></script>
<p><span>Sample Text:</span>Many people have sent language examples over the years, but we want more! We'd love to add more examples</p>
I am trying to remove / replace white space from a string in as3. The string comes from xml and than written into text field. to compare the strings I am trying to remove white spaces
var xmlSentence:String=myXML.SENTENCE[thisSentence];
var tfSentence=e.target.text;
var rex:RegExp = /\s+/;
trace(xmlSentence.replace(rex, "-"));
trace(tfSentence.replace(rex, "-"));
That code outputs like this:
She-has a dog
-She has a dog
I also tried different rex patterns. the problem is that though there are spaces in both string -which are same- it finds only one space but not the same one in both strings.
Could you help me to solve this problem
Thanks in advance
You need to use the g flag to indicate recursive changes
var rex:RegExp = /\s+/g ;
Within your Actionscript code, select the RegExp keyword, then goto the 'Help' menu and choose 'Flash Help' for more info on flags.
I'm writing a Chrome Extension, and I was wondering if it was possible to get the selected text of a particular tab, including the underlying HTML? So if I select a link, it should also return the <a> tag.
I tried looking at the context menu event objects (yes, I'm using a context menu for this), and this is all that comes with the callback:
editable : false
menuItemId : 1
pageUrl : <the URL>
selectionText : <the selected text in plaintext formatting, not HTML>
It also returns a Tab object, but nothing in there was very useful, either.
So I'm kind of at a loss here. Is this even possible? If so, any ideas you might have would be great. Thanks! :)
Getting the selected text of a page is fairly easy, you can do something like
var text = window.getSelection().toString();
and you'll get a text representation of the currently selected text that you can pass from a content script to a background page or a popup.
Getting HTML content is a lot more difficult, mostly because the selection isn't always at a clean HTML boundary in the document (what if you only select a small part of a long link, or a few cells of a table for example). The most direct way to get all of the html associated with a selection is to reference commonAncestorContainer, which is a property on a selection range that corresponds with the deepest node which contains both the start and end of the selection. To get this, you'd do something like:
var selection = window.getSelection();
// Only works with a single range - add extra logic to
// iterate over more ranges if needed
var range = selection.getRangeAt(0);
var container = range.commonAncestorContainer;
var html = container.innerHTML
Of course, this will likely contain a lot of HTML that wasn't actually selected. It's possible that you could iterate through the children of the common ancestor and prune out anything that wasn't in the selection, but that's going to be a bit more involved and may not be necessary depending on what you're trying to do.
To show how to wrap this all up into an extension, I've written a short sample which you can reference:
http://github.com/kurrik/chrome-extensions/tree/master/contentscript-selection/
If you don't want all of the siblings, just the selected HTML, use range's other methods like .cloneContents() (to copy) or .extractContents() (to cut).
Here I use .cloneContents():
function getSelectedHTML() {
var range = window.getSelection().getRangeAt(0); // Get the selected range
var div = document.createElement("div");
div.appendChild(range.cloneContents()); // Get the document fragment from selected range
return div.innerHTML; // Return the actual HTML
}