regex remove all non alphanumeric except # - javascript

How do I remove all non-alphanumeric characters except for #?
`#some random text goes here %#KG§ blah`.replace(/\W/g, ``) // replaces all non-alphanumeric but need to keep the #

/[^a-z0-9#]*/g should work for your case.
console.log(`#some random text goes here %#KG§ blah`.replace(/[^a-z0-9#]*/g, ``))
Quick explaniation
We're picking anything that is alphanumeric or '#' and picking any number of them using '*', then the '^' acts as a not.
I often use this site to test my regexp https://regex101.com/

Use a negated character class that lists # and word chars \w:
console.log('#some random text goes here %#KG§ blah'.replace(/[^#\w]/g, ''));

let str = 'I contains invalid char ##_&';
str.replace((/[^#a-z0-9]+/gi), '');
Please Note that some answers here won't replace something like underscores _
E.g
str.replace(/[^#\w]/g, ''))
Because \w matches underscores

Related

Javascript regular expression to search a string in word having multiple special characters (non space)

Requirement is to find if the search string is present in the given string with below conditions.
Condition 1 Search string should be found at the begin of the word i.e, no special characters preceding it.
abc should match in string that begins with abc like abcdef any where in the sentence.
abc should NOT match in xabcdef should NOT match as it is not starting with 'abc'
Condition 2 If the string is preceded with some special character, then it should also have some text before special characters.
abc should match in test_abcdef - as 'abc' is preceded with 'test_'
abc should NOT match in _abcdef - as it is starting with '_' without any text before _
Below regular expression is not finding abc if string has multiple special characters ex in string test#_abcdef or test__abcdef.
In the regular expression not sure how to add quantifier in '(?<=[A-Za-z0-9][^A-Za-z0-9])abc' where [^A-Za-z0-9] is checking for SINGLE non alpha numeric character.
What is the syntax to add 0 or more special character in reqex (?<=... )
Regular Expression tried in Online Regex Tester
/^(?<![^A-Za-z0-9])abc|(?<=[A-Za-z0-9][^A-Za-z0-9])abc|(?<=\ )abc/g
Sample Text :
abcdef abcdef _abcdef xabcdef test_abcdef test__abcdef abc
You can apply all the assertions without alternation here:
/(?<![a-z0-9])(?<!^[^a-z0-9])(?<!\s[^a-z0-9])abc/igm
RegEx Demo
This regex has 3 assertions before matching abc:
(?<![a-z0-9]): Fail the match when previous character is not alphanumeric
(?<!\s[^a-z0-9]): Fail the match when we have a non-alphanumeric character without preceding with some non-space character.
(?<!^[^a-z0-9]): Fail the match when we have a non-alphanumeric character at line start
Also note that lookbehind support in Javascript is still limited to new browsers only.
As regexes don't allow variable lenght lookbehind assertions, I don't think you can match just 'abc' but at the same time discard things like " _abc" " __abc" " ___abc", "____abc", etc.
I would suggest to do it in 2 steps:
First, try to match all required cases with a regexp without limiting yourself to just match 'abc'
(?:(?!abc[^a-zA-Z0-9\s]+)[a-zA-Z0-9]+[^a-zA-Z0-9\s]+|^|\s)(abc)
https://regex101.com/r/bAo05D/3
Then, just recalculate abc index with: abc_index = whole_regexp_index + length(regexp_matched_string) - length(abc)

Replace the special character and space from the string in javascript

I have a below string. I need to remove all the special character and space.
var Uid = "s/Information Needed1-s84102-p306";
I tried the below code.It didn't replace the space from the string.
console.log(Uid.replace(/[^\w\s]/gi, '')}")
The output is:- sInformation Needed1s84102p306
I want the output as sInformationNeeded1s84102p306
Simply try using
/[\W_]/g
\W match any non-word character [^a-zA-Z0-9_]
Included _ if you also want to remove it then
Regex
You can just use:
console.log(Uid.replace(/\W+/g, '')}")
\W will match any non-word character including a space.
RegEx Demo
You can use this expression for your case
var x = "s/Information Needed1-s84102-p306";
console(x.replace(/[^A-Z0-9]/ig, ""));
Here is the working Link

How to extract the last word in a string with a JavaScript regex?

I need is the last match. In the case below the word test without the $ signs or any other special character:
Test String:
$this$ $is$ $a$ $test$
Regex:
\b(\w+)\b
The $ represents the end of the string, so...
\b(\w+)$
However, your test string seems to have dollar sign delimiters, so if those are always there, then you can use that instead of \b.
\$(\w+)\$$
var s = "$this$ $is$ $a$ $test$";
document.body.textContent = /\$(\w+)\$$/.exec(s)[1];
If there could be trailing spaces, then add \s* before the end.
\$(\w+)\$\s*$
And finally, if there could be other non-word stuff at the end, then use \W* instead.
\b(\w+)\W*$
In some cases a word may be proceeded by non-word characters, for example, take the following sentence:
Marvelous Marvin Hagler was a very talented boxer!
If we want to match the word boxer all previous answers will not suffice due the fact we have an exclamation mark character proceeding the word. In order for us to ensure a successful capture the following expression will suffice and in addition take into account extraneous whitespace, newlines and any non-word character.
[a-zA-Z]+?(?=\s*?[^\w]*?$)
https://regex101.com/r/D3bRHW/1
We are informing upon the following:
We are looking for letters only, either uppercase or lowercase.
We will expand only as necessary.
We leverage a positive lookahead.
We exclude any word boundary.
We expand that exclusion,
We assert end of line.
The benefit here are that we do not need to assert any flags or word boundaries, it will take into account non-word characters and we do not need to reach for negate.
var input = "$this$ $is$ $a$ $test$";
If you use var result = input.match("\b(\w+)\b") an array of all the matches will be returned next you can get it by using pop() on the result or by doing: result[result.length]
Your regex will find a word, and since regexes operate left to right it will find the first word.
A \w+ matches as many consecutive alphanumeric character as it can, but it must match at least 1.
A \b matches an alphanumeric character next to a non-alphanumeric character. In your case this matches the '$' characters.
What you need is to anchor your regex to the end of the input which is denoted in a regex by the $ character.
To support an input that may have more than just a '$' character at the end of the line, spaces or a period for instance, you can use \W+ which matches as many non-alphanumeric characters as it can:
\$(\w+)\W+$
Avoid regex - use .split and .pop the result. Use .replace to remove the special characters:
var match = str.split(' ').pop().replace(/[^\w\s]/gi, '');
DEMO

JS & Regex: how to replace punctuation pattern properly?

Given an input text such where all spaces are replaced by n _ :
Hello_world_?. Hello_other_sentenc3___. World___________.
I want to keep the _ between words, but I want to stick each punctuation back to the last word of a sentence without any space between last word and punctuation. I want to use the the punctuation as pivot of my regex.
I wrote the following JS-Regex:
str = str.replace(/(_| )*([:punct:])*( |_)/g, "$2$3");
This fails, since it returns :
Hello_world_?. Hello_other_sentenc3_. World_._
Why it doesn't works ? How to delete all "_" between the last word and the punctuation ?
http://jsfiddle.net/9c4z5/
Try the following regex, which makes use of a positive lookahead:
str = str.replace(/_+(?=\.)/g, "");
It replaces all underscores which are immediately followed by a punctuation character with the empty string, thus removing them.
If you want to match other punctuation characters than just the period, replace the \. part with an appropriate character class.
JavaScript doesn't have :punct: in its regex implementation. I believe you'd have to list out the punctuation characters you care about, perhaps something like this:
str = str.replace(/(_| )+([.,?])/g, "$2");
That is, replace any group of _ or space that is immediately followed by punctation with just the punctuation.
Demo: http://jsfiddle.net/9c4z5/2/

How to trim all non-alphanumeric characters from start and end of a string in Javascript?

I have some strings that I want to clean up by removing all non-alphanumeric characters from the beginning and end.
It should work on these strings:
)&*#^#*^#&^%$text-is.clean,--^2*%#**)(#&^ --->> text-is.clean,--^2
-+~!##$%,.-"^&example-text#is.clean,--^#*%#**)(#&^ --->> example-text#is.clean
I have this regex, which removes them from the whole string:
val.replace(/[^a-zA-Z0-9]/g,'')
How would I change it to only remove from the beginning and end of string?
Modify your current RegExp to specify the start or end of string with ^ or $ and make it greedy. You can then link the two together with an OR |.
val.replace(/^[^a-zA-Z0-9]*|[^a-zA-Z0-9]*$/g, '');
This can be simplified to a-z with i flag for all letters and \d for numbers
val.replace(/^[^a-z\d]*|[^a-z\d]*$/gi, '');
You need to use anchors - ^ and $. And also, you would need a quantifier - *:
val.replace(/^[^a-zA-Z0-9]*|[^a-zA-Z0-9]*$/g,'')
Use anchors to match the start and end of the string:
val.replace(/^[^A-Z0-9]+|[^A-Z0-9]+$/ig, '')
Use anchors ^ and $ to match positions before first character and after last character in the string.
val.replace(/(^[^A-Za-z0-9]*)|([^A-Za-z0-9]*$)/g, '');
You can also shorten your code using \W which means non-alphanumeric character, shortcut for [^a-zA-Z0-9_] in case you want to keep underscore as well.
val.replace(/(^\W*)|(\W*$)/g, '');

Categories

Resources