Learn R Programming

xfun (version 0.48)

alnum_id: Generate ID strings

Description

Substitute certain (by default, non-alphanumeric) characters with dashes and remove extra dashes at both ends to generate ID strings. This function is intended for generating IDs for HTML elements, so HTML tags in the input text will be removed first.

Usage

alnum_id(x, exclude = "[^[:alnum:]]+")

Value

A character vector of IDs.

Arguments

x

A character vector.

exclude

A (Perl) regular expression to detect characters to be replaced by dashes. By default, non-alphanumeric characters are replaced.

Examples

Run this code
x = c("Hello world 123!", "a  &b*^##c 456")
xfun::alnum_id(x)
xfun::alnum_id(x, "[^[:alpha:]]+")  # only keep alphabetical chars
# when text contains HTML tags
xfun::alnum_id("Hello world!")

Run the code above in your browser using DataLab