当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Remove everything not matched by regex

问题描述:

I am trying to create a visualisation that removes non punctuation characters, but still keeps track of chapters in the string.

So far I achieved to create the following regex to match what I want to preserve:

(\CAPÍTULO ([0-9]+))|([\\?.,:;!¡¿。、·*\(\)\[\]\-–\_«»\'\'\/@#$&\%\^=+\|<>\"])

How can I do to replace the rest of the text from a string?

网友答案:

You can easily use the replace function, try this for an example:

var str = "whatever, string, you like!";
var newStr = str.replace(/\w/g, ' ');

console.log(newStr);
网友答案:

Try this:

var r = /(CAPÍTULO [0-9]+|[\\?.,:;!¡¿。、·*\(\)\[\]\-–\_«»\'\'\/@#$&\%\^=+\|<>\"])|(.)/g
var s = "[email protected]#123^&*XYZ;";
var p = s.replace(r, "$1");

// Result: "[email protected]#^&*;"

First, it matches all characters in group 1, and any non-matches fall into the second group. Since the result of a group number (i.e. $1) is empty if not found, this effectively clears everything except the first group matches.

If you need to preserve the placement of the other characters you could do this:

var r = /(CAPÍTULO [0-9]+|[\\?.,:;!¡¿。、·*\(\)\[\]\-–\_«»\'\'\/@#$&\%\^=+\|<>\"])|(.)/g
var s = "[email protected]#123^&*XYZ;";
s.replace(r, "$1,").split(",");

// Result: ["", "", "", "!", "@", "#", "", "", "", "^", "&", "*", "", "", "", ";", ""]

You just need to handle the extra "" at the end. ;) This works because instead of replacing with nothing, (i.e. "$1" becomes "" for non-matches), the comma (or any character you want really), keeps track of the positions. You could use anything, such as s.replace(r, "$1~").split("~"); also.

网友答案:

removes non punctuation characters, but still keeps track of chapters in the string.

The classic way to do this is to match on the thing you do want to keep (CAPITULO [0-9]+), OR (|) other things you don't want to keep--in this case, non-punctuation characters (\w), and capture that, then replace the latter with an empty string.

const regexp = /CAPITULO [0-9]+|(\w)/g;

const input = "CAPITULO 22 .#( nonpunctuation characters $%&*'";

const processed = input.replace(regexp, (match, match1) => match1 ? '' : match);

console.log(processed);
分享给朋友:
您可能感兴趣的文章:
随机阅读: