implementing address http in searchbar - string

I would need some help. Basically I have a searchbar, in this searchbar I already have to preset a link that will lead to a specific search on google. The user in the search bar will write, for example, "dog" and will be brought directly to the search results on google gif. They own the http address that leads to search results. In the text below, replace the word "lolcats" with the word entered by the user in the searchbar taking into account the spaces that are recognized by% 20
I hope I'm well explained with this poor English
example link ("lolcats" Word to be replaced with the one that inserts the user into the searchbar)
https://www.google.it/search?as_st=y&tbm=isch&hl=it&as_q=lolcats&as_epq=&as_oq=&as_eq=&cr=&as_sitesearch=&safe=images&tbs=itp:animated,ift:gif#imgrc=_

try to create your url string like this:
var keyWord : String // which you get from user
let part1 = "https://www.google.it/search?as_st=y&tbm=isch&hl=it&as_q="
let part2 = "&as_epq=&as_oq=&as_eq=&cr=&as_sitesearch=&safe=images&tbs=itp:animated,ift:gif#imgrc=_"
var encodedKeyWord = keyWord.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
var finalUrlString = part1 + "\(encodedKeyWord)" + part2
hope it works :S

Related

Find and replace text and wrap in "href"

I am trying to find specific word in a div (id="Test") that starts with "a04" (no case). I can find and replace the words found. But I am unable to correctly use the word found in a "href" link.
I am trying the following working code that correctly identifies my search criteria. My current code is working as expected but I would like help as i do not know how to used the found work as the url id?
var test = document.getElementById("test").innerHTML
function replacetxt(){
var str_rep = document.getElementById("test").innerHTML.replace(/a04(\w)+/g,'TEST');
var temp = str_rep;
//alert(temp);
document.getElementById("test").innerHTML = temp;
}
I would like to wrap the found word in an href but i do not know how to use the found word as the url id (url.com?id=found word).
Can someone help point out how to reference the found work please?
Thanks
If you want to use your pattern with the capturing group, you could move the quantifier + inside the group or else you would only get the value of the last iteration.
\ba04(\w+)
\b word boundary to prevent the match being part of a longer word
a04 Match literally
(\w+) Capture group 1, match 1+ times a word character
Regex demo
Then you could use the first capturing group in the replacement by referring to it with $1
If the string is a04word, you would capture word in group 1.
Your code might look like:
function replacetxt(){
var elm = document.getElementById("test");
if (elm) {
elm.innerHTML = elm.innerHTML.replace(/\ba04(\w+)/g,'TEST');
}
}
replacetxt();
<div id="test">This is text a04word more text here</div>
Note that you don't have to create extra variables like var temp = str_rep;

Adding a "+" to a " " in node.js for a movie api

I know this will be a simple question. I am new coding and still have a lot to learn. I am running a movie API with node. As you know API's searches cannot have spaces " " and need a plus sign "+" in order to search a string. Example when I search Die Hard on the terminal it comes back as a movie called Die and does not recognize "Hard". If I search it as Die+Hard I will get the movie I am looking for. How do I add that plus sign without having the user write the plus sign in the search? Thank you for your help.
var axios = require("axios");
movieName = (process.argv[2]);
var queryUrl = "http://www.omdbapi.com/?t=" + movieName + "&y=&plot=short&apikey=...";
To replace all instances of a space in a string (let's call it str) with +:
str.replace(/ /g, "+");
To replace all instances of any whitespace characters with a +:
str.replace(/\s/g, "+");
For more information, see the MDN docs on String.prototype.replace().
var movieName = process.argv.slice(2).join("+");
this took care of it. Thank you for the help.

Custom Browser Search Engines and Multiple Parameters

I have been working with custom search engines in Chrome and Vivaldi, which have been absolutely fantastic. However, I have only been able to successfully perform searches that replace a single parameter (e.g., the %s in the following URL: http://www.thesaurus.com/browse/%s) with my search term (e.g., http://www.thesaurus.com/browse/trifocal).
Is it possible to replace multiple parameters for a custom browser search engine so I could not only specify a search term but also the webpage to search? For example, I would type the custom search engine shortcut "d" followed by "thesaurus, trifocal", which would input the two parameters into a predefined URL and search the word "trifocal" on "thesaurus.com".
Basically, I want to be able to search for multiple values at two different points in the custom search engine for scenarios where multiple sites use the same "base" url where the only difference is a word or two.
Please let me know if you have any questions.
You can use JavaScript. Take the entire string as input to the function. Split it based on some character and them trim the parts. Construct the link and then open it within the current tab or in a new tab, depending on which suits you.
javascript:(function myFunction(str) {
var part = ";";
var res = str.split(part);
var search_query = res[0].trim();
var category = res[1].trim();
var new_tab = res[2];
var res_url = "https://github.com/search?q=" + search_query + "&type=" + category;
// open in new tab if str contains a third parameter that is composed of zero or more spaces
if ((new_tab !== undefined) && (new_tab.trim() === ""))
{
window.open(res_url, "_blank");
}
else // open in current tab
{
window.location.href = res_url;
}
})('%s');
On minifying, the custom search engine URL would be javascript:(function myFunction(str){var part=";";var res=str.split(part);var search_query=res[0].trim();var category=res[1].trim();var new_tab=res[2];var res_url="https://github.com/search?q="+search_query+"&type="+category;if((new_tab!==undefined)&&(new_tab.trim()==="")){window.open(res_url,"_blank")}else{window.location.href=res_url}})('%s');. The function is called with '%s' as the parameter. It splits based on the semicolon character.
Usage: Let's nickname this custom search engine as, say, "gm". Then a search for "gm binary tree; code" will open https://github.com/search?q=binary%20tree&type=code in the current page. A search for "gm radix tree; commits; " will open https://github.com/search?q=radix%20tree&type=commits in a new tab.
Note that this function would fail if %s contains single quote(s). It also doesn't work if called from an internal page like Settings.
An alternative to accomplish your goal would be to ditch the custom search engine idea and use a scripting tool like AutoHotkey (Windows) to replace search strings by URLs.

How to extract youtube url from string in Typescript

I have a text like
string 1 :
I am a text and this is a youtube video https://www.youtube.com/watch?v=j82FBbgpUy4 !!!
I would like to extract the youtube video url from the string and return it.
Result :
string 2 :
https://www.youtube.com/watch?v=j82FBbgpUy4
Is there another way more efficient than spliting the string by spaces and searching in the array if it contains "https://www.youtube.com/watch?v=" (it's not the best way to do it)? I cannot find any other solution
So. I found this regular expression http(?:s?):\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w\-\_]*)(&(amp;)?‌​[\w\?‌​=]*)?, here, which seems to work.
Once you have that, all you have to do is this:
var re = /http(?:s?):\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w\-\_]*)(&(amp;)?‌​[\w\?‌​=]*)?/;
var str = "I am a text and this is a youtube video https://www.youtube.com/watch?v=j82FBbgpUy4 !!!"
var url = re.exec(str)['0']
Then url should have the video url.

Dynamic Strings, AS2, needed on one line

Ok, so I'm creating a Flash HUD in AS2 that runs on the Surface and connects to our server.
As it stands now, I'm having to hard code the IP addresses for the Surface to connect to, and I'm trying to get past this.
I have 4 text fields for the user to enter the 4 fields of IP address data. My issue at the moment is that if I set the String variable literally, it works fine. But if I dynamically create the string, instead of outputting on one line, it outputs each of the 4 strings separately.
Here's my code:
var newIP1 = getIP.IPtext.IP1.text; //grabbing the data from the UI
var newIP2 = getIP.IPtext.IP2.text;
var newIP3 = getIP.IPtext.IP3.text;
var newIP4 = getIP.IPtext.IP4.text;
var ipArray = new Array(newIP1,newIP2,newIP3,newIP4); //setting the array
trace (ipArray.join(".")); // output the string, replacing the commas with a period
//output:
//10
//.255
//.255
//.22
//If I do this it works fine
var IPstr = "10.255.255.2";
trace(IPstr);
// output: 10.255.255.22
I appreciate any help on this, thanks in advance.
Your code looks good and should work as expected.
One thing to check would be to see if there isn't a carriage return or newline character being added to each individual input box. One way to check would be to check the length of each of your input strings to ensure there isn't an invisible character there.

Resources