Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove everything but all regex matches #156

Open
Piero512 opened this issue Aug 24, 2020 · 2 comments
Open

Remove everything but all regex matches #156

Piero512 opened this issue Aug 24, 2020 · 2 comments

Comments

@Piero512
Copy link

Piero512 commented Aug 24, 2020

I have written a script that interprets the selected text as a RegExp, and replaces fullText with the text matched by the regexp joined with newlines, but I can't get it to work.

/**
	{
		"api":1,
		"name":"Regex select",
		"description":"Use selection as regex input, removes it from the input and then returns all matches",
		"author":"piero512",
		"icon":"quote",
		"tags":"boop,state,script,debug,new,create"
	}
**/

function removeRegexInput(selected, text) {
    return text.replace(selected, '');
}
function copyRegexMatches(regexp, text) {
    return text.match(regexp);
}
function main(input) {
    try {
        if (!input.isSelection) {
            input.postError('You have to select something before calling this script!');
        }
        else {
            var copy_selection = input.selection;
            var removedRegex = removeRegexInput(input.selection, input.fullText);
            var regexp = RegExp(copy_selection, 'gm');
            var matches = copyRegexMatches(regexp, removedRegex);
            var newText = matches.join('\n');
            console.log(newText);
            input.fullText = newText;
        }
    }
    catch (e) {
        input.postError("Unknown error: " + e.toString());
    }
}
@zoeyfyi
Copy link
Contributor

zoeyfyi commented Aug 24, 2020

!input.isSelection should be !input.selection. In case you didn't know, there is instructions to debug scripts.

I like the idea of this script, would be great with #122.

@Piero512
Copy link
Author

Well, I should have added what was wrong. The else branch runs when there is a selection (as is), but the expected behavior is to replace the text in the window completely with the newText var but it just doesn't work for me 😢,nor it throws exceptions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants