views:

54

answers:

2

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, paranthesis 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

+1  A: 

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 like stereofrog said,

yourstring=yourstring.replace(/\s*\(\d+\)/g, "")
S.Mark
or just yourstring.replace(/\(\d+\)/g, "")
stereofrog
ummm as I can read .. you assume that my string starts with s, I've edited the question to make my question more undrestandable(I hope).tnx
Gandalf StormCrow
no, its not only for string starts with s, \s is whitespace which include space not the char "s"
S.Mark
+1  A: 

Here it is:

var text = "Java (77) Javascript (22) Car (11) Carpet (15)";
text.replace(/(\w+) \(\d+\)/g,"$1");   // returns "Java Javascript Car Carpet"
Andrea Zilio
Great , that is exactly what I was looking for :D
Gandalf StormCrow