0

The value of selected variable is "Topic [9]" or "Category [10]". i want to remove "[number]" from the value of selected variable?

selected = $('#topic_id option:selected').text();

i think, i can strip value with replace() as follows but need to know, how do i achieve because number is not fixed.

selected = $('#topic_id option:selected').text().replace();
3
  • There are a number of ways to do this, but you need to provide more info, if it is always going to be one word and then a number, you can split() and take the first part, or you can detach after the word. Commented Jun 30, 2014 at 20:54
  • 1
    You could just find the index of the "[", and remove all the characters that follow. Something like (and I know this syntax isn't correct) newVal = left(string,indexOf(string."[")) Should give you all the characters to the left of where that bracket appears. Commented Jun 30, 2014 at 20:54
  • I think @DontVoteMeDown has the good approach. Thank you for taking time. Commented Jun 30, 2014 at 20:57

3 Answers 3

4

This has nothing to do with jQuery but with pure Javascript. Just use regex, like this one \[.+ to do it.

selected = $('#topic_id option:selected').text().replace(/\[.+/g, "");

Demo.

Of course this only works on the pattern you have provided: With [number] at the end of the string.

UPDATE: As @Zack pointed on the comments, you can use this regex \s\[.+ to remove the space before the [.

Sign up to request clarification or add additional context in comments.

11 Comments

Regex is going way to far, your making it for more complicated then it needs to be.
@Ohjay44 WTF? I think you would propose split()? Come on! hahaha
@Ohjay44 That's a ridiculous oversimplification. Using a simple regex does not complicate anything unless you don't know how to use them.
Your regex replace will just remove everything after the first [ right? Wouldn't you need to remove the space before it as well? .replace(/\s\[.+/g, "");
@DontVoteMeDown is it my limited knowledge, or your closed mind to the other possibilities? I would be right in saying that there are a number of ways to do one thing when programming, there are easier ways, and complicated ways. So maybe it's that your to comfortable doing something one way that you can't accept the possibility that there is a much simpler way. If it were only one word he needed then yes split() would have been quick. Now that I know he might have more then one, then yes Regex works best, but it is still not the only way.
|
3

Ohjay44 was onto something with the split, but didn't provide an answer, so I went ahead. This solution will split the selected string by the space character, then call pop() which removes the last element of the array, then it uses join(" ") which returns the elements of an array as a string, concatenated with the provided seperator, or , if none is provided.

Here is a working example on jsfiddle.net http://jsfiddle.net/jX36T/

var selected = "Topic of conversation [9]";
var splitted = selected.split(" ");
splitted.pop();
var string = splitted.join(" ");
alert(stringResult);

Displays "Topic of conversation"

Comments

0

If you only have one word to get, you can explode with the space character and keep the first item of the returned array

2 Comments

but value can have more than one word
.split(' ') (not explode) If more than one word, you can get all the items of the array, but not the last one

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.