1

I have a string of text that I am trying to remove from a textarea in javascript. The text that I am trying to remove is in the following format:

Category:Attribute

Color:Green <- Example!

var catTitle = 'color';
var regexp = new RegExp(catTitle + '\:[.]*$', "g")
textarea.value = textarea.value.replace(regexp, "");

Since the attribute can be any length, I need my regular expression to go forward until the new line. I thought that [.]*$ would be acceptable to match any characters up to a new line; however, it doesn't seem to work.

Any help would be greatly appreciated.

asked Jun 12, 2011 at 19:34
0

2 Answers 2

2

. inside a character class ([]) matches itself -- it is no longer a wildcard. Also, regular expressions are case sensitive unless told otherwise ("i" flag) and : (in that context) is not special so it does not need to be escaped.

[a-z] or \w might be good to use to match the Attribute, depending.

It might then go like:

var catTitle = "color"
var re = new RegExp(catTitle + ':[a-z]+$', "gi")
re.test("Color:Green") // true
re.test("color:Red") // true
re.test("color: Red") // false, put perhaps this should be accepted?
re.test("color:") // false
re.test("foo:bar") // false

I also changed the qualifier from * to + -- note the case of "color:" and how it's rejected. This will also reject colors in non-English alphabets.

Happy coding.

answered Jun 12, 2011 at 19:38
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for your response - I will mark it as the answer. Once the 10 minutes is up.
Wait a few hours (or more), then make a choice :)
0

Or, since you may have more lines:

var catTitle = 'Color';
var regexp = new RegExp(catTitle + ':.*', "g")
alert("skzxjclzxkc\nsasxasxColor:Red\nasdasdasd".replace(regexp, ""));
answered Jun 12, 2011 at 19:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.