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

3
  • 4
    You should better explain the expected format of something. For example "something is single alphanumeric word", or "something is a one or at most two words starting with a digit". Commented Jan 23, 2010 at 13:49
  • The way your question is phrased: s = "something"; s = s + "something (22)" Commented Jan 23, 2010 at 13:54
  • 2
    What Andrea said. Now an answer like: "by performing some regex or other string manipulation" would be as good as an answer as the question is. :) Commented Jan 23, 2010 at 13:55

2 Answers 2

1

Here it is:

var text = "Java (77) Javascript (22) Car (11) Carpet (15)";
text.replace(/(\w+) \(\d+\)/g,"$1");   // returns "Java Javascript Car Carpet"
Sign up to request clarification or add additional context in comments.

Comments

1

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, "")

2 Comments

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
no, its not only for string starts with s, \s is whitespace which include space not the char "s"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.