PierceJ
PierceJ

Reputation: 95

Removing only certain types of characters regex

Edit: Sorry, should have given more information

Is it possible to replace only the english text here?

{ "question": "本能", "answers":[ "ほんのうInstinct" ] },

I want it to just be { "question": "本能", "answers":[ "めがね" ] }, and keep the other english text.

Tried [A-Za-z]+ and it works for all english text, but I don't know how to select a certain section. I'm using find and replace on notepad+

Upvotes: 2

Views: 109

Answers (3)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626896

You may use

(?:\G(?!^)|"answers"\s*:\s*\[\s*")[^"]*?\K[a-zA-Z]+

and replace with an empty string.

Details:

  • (?:\G(?!^)|"answers"\s*:\s*\[\s*") - the end of the previous successful match (\G(?!^)) or (|) "answers" substring, 0+ whitespaces, :, 0+ whitespaces, [, 0+ whitespaces, " (see "answers"\s*:\s*\[\s*")
  • [^"]*? - any 0+ chars other than ", as few as possible (as *? is a lazy quantifier)
  • \K - a match reset operator discarding all text matched so far from the match buffer
  • [a-zA-Z]+ - 1 or more ASCII letters

Test:

enter image description here

Upvotes: 1

Ulysse BN
Ulysse BN

Reputation: 11396

You can replace everything that matches /[a-z]/gi by an empty string.

The flags g and i respectively means that you want to match multiple times and you want to be case insensitive.

You also could use \w shortcut which stand for word character, but this will also match [0-9_] which you may not want to delete.

Here is a working exemple of the regex.

Upvotes: 1

jdubjdub
jdubjdub

Reputation: 613

this is your regex: /[A-Za-z]*/g

var s = '":[ "めがねGlasses" ] },'

console.log(s.replace(/[A-Za-z]*/g, ''))

Upvotes: 1

Related Questions