Clearing the player of an item that does NOT have a custom name on it

I recommend using a data pack for custom crafting. Here (archive) is the wiki article for custom recipes. These are made exactly for your usecase and can do everything a normal person would want from a custom crafting system.

I'll answer your question anyway: The problem is that you need to specify every slot. If you also want to replace the unnamed items with ones that have special properties, while keeping the count (useful for shift-clicking in custom crafting), you also need to specify every possible amount. That gives you a total of 2304 commands in the worst case. But you can mainly just copy-paste. There are also some ways to improve performance, if you need that, but it should be ok as it is.
This does not work for arbitrary banner patterns, because you would need to specify more possible combinations than you have hard drive space for.

Here are the commands that replace all wooden swords that are not named "a" with wooden swords that are named "b". You can just remove the »Name:"{\"text\":\"a\"}"« part if you want to replace all wooden swords that have no name and no lore.

execute as @p if data entity @s Inventory[{Slot:0b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s hotbar.0 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:1b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s hotbar.1 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:2b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s hotbar.2 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:3b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s hotbar.3 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:4b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s hotbar.4 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:5b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s hotbar.5 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:6b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s hotbar.6 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:7b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s hotbar.7 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:8b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s hotbar.8 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:9b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.0 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:10b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.1 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:11b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.2 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:12b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.3 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:13b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.4 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:14b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.5 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:15b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.6 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:16b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.7 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:17b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.8 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:18b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.9 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:19b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.10 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:20b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.11 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:21b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.12 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:22b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.13 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:23b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.14 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:24b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.15 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:25b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.16 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:26b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.17 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:27b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.18 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:28b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.19 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:29b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.20 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:30b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.21 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:31b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.22 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:32b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.23 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:33b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.24 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:34b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.25 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:35b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.26 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1

Explanation: For every slot, the corresponding command checks first if you have exactly 1 (in case you want to use different items, you can copy-paste this and change the amounts, otherwise you might as well leave out the »,Count:1b« part and the "1" at the end) wooden sword in that slot, then it checks if you have not a wooden sword with the name "a" in that slot, then it replaces the item. So if you have a wooden sword there which is named "a", nothing happens. And if you don't have a wooden sword there, nothing happens as well.
You can also chain multiple of these unless data parts to exclude multiple names.


I do not think that there is a way to do it and no easy workaround either.

If it is possible for you to tag every sword, then you can use this to give a player a sword that does not have a custom name:

/give <player> minecraft:wooden_sword{canBeDeleted:true}

Which you then can delete with:

/clear @a minecraft:wooden_sword{canBeDeleted:true} 1

This would only delete swords with a "canBeDeleted" tag, so if you create a sword with a custom name without that tag, then it will not be affected.