开发者

javascript regular expression to remove trailing numbers in parenthesis

开发者 https://www.devze.com 2022-12-17 02:50 出处:网络
How can I replace/append something (22) string to something, using some kind of expression matching something is not a constant its always different but the part (integer) is always the same. cheers

How can I replace/append something (22) string to something, using some kind of expression matching something is not a constant its always different but the part (integer) is always the same. cheers

EDIT

I see that this something is a bit confusing(for me as well). Here is what I mean. I have a string - a word . Which contains alpha expression(a word, any word) one single space opening and closing paranthesis and inside of the an intiger. Now I'd like to replace blank space, para开发者_JS百科nthesis and integer inside to replace with nothing and leave just the alpha part of the string.

Ex:

Java (77)
Javascript (22)
Car (11)
Carpet (15)

After regex it should look like this :

Java
Javascript
Car
Carpet

Note, the regex is performed on each word seperatly, I just wrote couple of examples to make things more clear to read and understand. Thank you


Here it is:

var text = "Java (77) Javascript (22) Car (11) Carpet (15)";
text.replace(/(\w+) \(\d+\)/g,"$1");   // returns "Java Javascript Car Carpet"


Assuming something as \w -> A-Za-z0-9_, this will convert something (22) to something

yourstring=yourstring.replace(/(\w+)\s*\(\d+\)/g,"$1")

or just remove numbers in brackets

yourstring=yourstring.replace(/\s*\(\d+\)/g, "")
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号