Showing posts with label regular expression. Show all posts
Showing posts with label regular expression. Show all posts

Wednesday, November 18, 2009

C# Regular Expressions

'*' Matches 0 or more patterns.
(e.g. "*[a]" matches 0 or more 'a's)

'+' Matches 1 or more patterns.
(e.g. "+[a]" matches 1 or more 'a's)

'?' Matches 0 or 1 pattern.
(e.g. "?[a]" matches 0 or 1 'a')

'^' Ignores a pattern.
(e.g. "^a" ignores 'a's)
Also, can be used to signify the start of a string.
(e.g. "^[0-9]" will match a string starting with number)

'[]' Used for range patterns.
(e.g. "[0-9]" matches any number between 0 and 9)

'.' will match any character except '\n'.
(e.g. "a.." matches any 3 letter word starting with 'a')

'\d' matches a digit.
(e.g. "\d"matches any 1 character number)

'$' matches the end of a string.
(e.g. "$[a]" matches a string ending in a)


Saturday, May 9, 2009

Replacing characters within strings with Javascript

I found out today how to replace characters within a string using Javascript.

It's not hard.

First, you take the string that has characters that need replacing.

You make a regular expression of the characters that need replacing. -> this uses the javascript RegEx("string") function.

You make a variable to hold the new string that is generated from the replacing.

You have a new string with replaced characters.

In the following example, I wanted to replace a string with the empty string, removing the unwanted characters.



temp will have the value of  "1,,3,4".

If you want to take the comma out, also, just put "2," into the regular expression function.

The regular expression function generates a regular expression that is understandable to javascript.  The first code would generate /2/ while the second code would generate /2,/.

Hope this helps.