1

First question : I want to replace all characters other than alphanumeric and Special Letters. For example , somestringğüş iöç123456!@#$%^&*()_+ to somestringğüş iöç123456

Second: For example , some---example--long-string to some-example-long-string

I do not really know regexp , so I need 2 simple regexp strings.Thank you

4
  • NO, I am just trying to create some share.php script (as iframe modal box . part of my project). Commented Jul 8, 2010 at 10:19
  • No offense. You gave both the question and a hint for the answer. Using Regexp you can replace the special characters. Commented Jul 8, 2010 at 10:20
  • if it looks like homework, if it reads like homework, it must be a homework. Please, leave tags alone. Commented Jul 8, 2010 at 10:33
  • 1
    I explained that , This is not homework This is part of my own project. If you want to feel like this is homework , There is no need to special 'homework' tag , please do not edit my question , If you want to feel it like homework do it yourself. Unfortunately , I can not understand Homework misconception of ST Community. Commented Jul 8, 2010 at 10:36

2 Answers 2

3

First. It matches any character that is not alphanumeric, whitespace or non-ascii, and replaced them with the empty string.

str.replace(/[^a-z0-9\s\x80-\uFFFF]+/gi, '');

There are no unicode-classes that I can use, so either I include all unicode characters, or list the ones that are not letters, digits nor whitespace.

Second. It matches any sequence of two or more dashes, and replaces them with a single dash.

str.replace(/-{2,}/g, '-');
Sign up to request clarification or add additional context in comments.

3 Comments

ğ and ş are outside of the \x80-\xff block.
You can combine these replaces: str.replace(/[^a-z0-9\s\x80-\uFFFF]+/gi, '-')
@Gumbo: Not quite, because that would introduce a dash in a###b instead. That may be okay, of course, but that's not what's specified. It is possible to do this in one regex nonetheless, but I'm not sure if it's worth it, and I'm not sure what OP wants a#-#-#-#b to become (i.e. a---b or a-b?)
3
 /* 1. */   return x.replace(/[!@#$%^&*()_+]/g, '');
 /* 2. */   return x.replace(/-{2,}/g, '-');

5 Comments

two or more hyphens should be replaced with empty string.
You were right that first time: that should be replace(/-{2,}/g, '-'). some---example --> some-example
@Silent & @Kobi : Actually There is no difference , They both do same thing . I just tested.
@oguz - I don't see how that's possible, that'd change a---b to ab, not a-b.
@Kobi: you're right, @Kenny: sorry, don't know what was I thinking

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.