I’m trying to escape the subset of markdown that Discord supports (*
, _
, `
, ~
). Characters that are already escaped should not have additional backslashes added. This is what I have:
function escapeMarkdown(text) { return text.replace(/([^\\]|^|\*|_|`|~)(\*|_|`|~)/g, '$1\\$2'); } console.log(escapeMarkdown('*test* _string_ ~please~ `ignore` *_`~kthx \* \\~'));
Run code snippetExpand snippet
This works fine, minus the fact that multiple markdown characters against each other will not all be escaped. I’m not sure how to expand this to allow for that, without making the expression absurdly complicated.
Related Posts:
- Javascript split regex question
- How to make a word underline in Markdown
- How to make a word underline in Markdown
- What is a good regular expression to match a URL? [duplicate]
- What is a good regular expression to match a URL? [duplicate]
- Is there a difference between /\s/g and /\s+/g?
- RegEx Ignore Case
- JavaScript replace/regex
- Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters
- How to test valid UUID/GUID?
- Regular expression : match either of two conditions?
- RegExp in TypeScript
- Converting any string into camel case
- I want to remove double quotes from a String
- Regular expression for removing whitespaces
- Javascript Regular Expression Remove Spaces
- How can I extract a number from a string in JavaScript?
- Remove not alphanumeric characters from string
- Extract hostname name from string
- Difference between ?:, ?! and ?=
- Remove all special characters with RegExp
- How do you use a variable in a regular expression?
- How to count string occurrence in string?
- Regular Expression to reformat a US phone number in Javascript
- Jquery Value match Regex
- RegEx: How can I match all numbers greater than 49?
- Regular expression to validate US phone numbers?
- javascript includes() case insensitive
- RegEx for Javascript to allow only alphanumeric
- Check user logged in with JS
- How to deal with the confliction between PHP-Markdown and MathJax?
- How do I check if an array includes a value in JavaScript?
- JavaScript document.getElementById().value logs empty value
- What is console.log?
- Can I use complex HTML with Twitter Bootstrap’s Tooltip?
- Compare two dates with JavaScript
- How can I merge properties of two JavaScript objects dynamically?
- In reactJS, how to copy text to clipboard?
- SyntaxError: Adjacent JSX elements must be wrapped in an enclosing tag
- Uncaught SyntaxError: Invalid or unexpected token
- JavaScript TypeError: Cannot read property ‘style’ of null
- Invalid shorthand property initializer
- nodejs – error self signed certificate in certificate chain
- How to read xml file contents in jQuery and display in html elements?
- using lodash .groupBy. how to add your own keys for grouped output?
- Uncaught ReferenceError: google is not defined when trying to use Google Places API without a map
- Why is window.showModalDialog deprecated? What to use instead?
- Should I use JSLint or JSHint JavaScript validation?
- How can I print using JQuery
- Angular: conditional class with *ngClass
- Failed to compile. Module not found: Can’t resolve ‘react-router-dom’
- How to send an email from JavaScript
- MissingSchemaError: Schema hasn’t been registered for model “User”
- classical inheritance vs prototypal inheritance in javascript
- How to measure time taken by a function to execute
- var.replace is not a function
- Uncaught TypeError: Cannot read property ‘call’ of undefined at __webpack_require__
- Async/await in componentDidMount to load in correct order
- This.props.dispatch not a function – React-Redux
- this vs $(this)
- How to open a URL in a new Tab using JavaScript or jQuery?
- How to sort an object array by date property?
- React router not showing browser history
- How to sort an array of integers correctly
- Failed to execute ‘postMessage’ on ‘DOMWindow’: https://www.youtube.com !== http://localhost:9000
- How can I check whether a radio button is selected with JavaScript?
- JavaScript button onclick not working
- What counts as CPU Intensive tasks (eg. sorting, searching etc?)
- What is the equivalent of Java’s System.out.println() in Javascript?
- How can I exit from a javascript function? [duplicate]
- Object doesn’t support property or method ‘addEventListener’
- How to fix javascript .toFixed is not a Function error
- Implementing Isomorphic JavaScript (React JS) in WordPress?
- Any advantage of using wp_scripts and is_IE when enqueuing scripts
- TinyMCE in a div / textarea on frontend?
- Include Javascript as Plain (No file inclusion)
- Adding wp_ajax to a theme outside functions.php (on specific template page)
- Insert shortcode in post editor from javascript (Visual / HTML)
- WordPress problem with htmlentities
- Provide specific example for block variation
- Gutenberg passing block attributes to component in ES6/ESNext
- How to add a custom.js file for a child-theme in a swift way?
- WP REST API Post Status Using JavaScript
- var is undefined in a Gutenberg block
- Custom wp.editor.initialize settings ignored
- Why my javascript code not working for the dashboard of wordpress admin?
- Got a TypeError after update and need to adapt found solution
- How to add classes and events to image in javascript using Gutenberg?
- How do you filter the list of states when country is selected? [closed]
- Prevent update_checkout from firing mid-typing
- Adding Code into Theme Customizer Header
- Suddenly extra hex string in tags
- Including Styles and JS files to work ON my plugin interface
- Gutenberg – useEffect manipulate DOM after block re-renders DOM
- Why does my array sort order changes when I pass it to JS using WP_REST_Response?
- Spans in gutenberg
- Get a default customizeAction text for a section using Customizer JS API
- custom page url slug needs illegal ?id=1 for javascript
- Use one javascript variable into another javascript file
- How can I look at/edit what’s being sent to post.php when I hit the publish/update button?