Noob Regex/Autohotkey

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
flubber
Posts: 3
Joined: 13 Feb 2018, 05:01

Noob Regex/Autohotkey

13 Feb 2018, 05:09

Hey All,

Hope you can help a bit of an Autohotkey Noob here.

Here is what I would like to do.

1) Manually highlight some text
2) Press a function key eg F1
3) Run the follow regex on the highlighted text and return the results to the clipboard


The function/regex that I need to run is this. Basically it turns the text into a url friendly "slug". I can use this in js but I have no idea how to do this in Autokey.

Code: Select all

 function slugify(text)
{
  return text.toString().toLowerCase()
    .replace(/\s+/g, '-')           // Replace spaces with -
    .replace(/[^\w\-]+/g, '')       // Remove all non-word chars
    .replace(/\-\-+/g, '-')         // Replace multiple - with single -
    .replace(/^-+/, '')             // Trim - from start of text
    .replace(/-+$/, '');            // Trim - from end of text
}
Any help/advice/examples would be greatly appreciated :thumbup:
trust_me
Posts: 98
Joined: 29 Jul 2017, 10:46

Re: Noob Regex/Autohotkey

13 Feb 2018, 12:32

just a quick try:

Highlight text and F4

Code: Select all

f4::
clipboard=
send ^c
ClipWait
msgbox % slugify(clipboard)
return

slugify(z){
return regexreplace(trim(regexreplace(Format("{:L}",z)," ","-"),"-"),"[-]{2}")
}
flubber
Posts: 3
Joined: 13 Feb 2018, 05:01

Re: Noob Regex/Autohotkey

13 Feb 2018, 12:38

Oh my.. That's awesome. Thank you so much.
WouterVS

Re: Noob Regex/Autohotkey

25 Apr 2018, 20:31

The code, ready for copy paste :)

Code: Select all

Slugify(str) {
	str := Trim(Format("{:L}", str))
	str := RegexReplace(str, "\s+", "-")
	str := RegexReplace(str, "[^\w-]+", "")
	str := RegexReplace(str, "--+", "-")
	str := RegexReplace(str, "-+$", "")
	str := RegexReplace(str, "^-+", "")
	return str
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ArkuS, Chunjee, Frogrammer and 368 guests